Silentrash
Silentrash

Reputation: 45

Logging count value in histogram, not the axis itself

I made this example to show what I am trying to do:

data = np.random.normal(loc=1000,scale=20,size=2000)
plt.hist(np.log10(data),log=True)
plt.xlabel('Log(data)')
plt.ylabel('Count')
plt.show()

After executing this command, I get a nice histogram with Log(data) on x-axis and Counts on y-axis (y-axis is log scaled). For some reason, I want Log(counts) on y-scale, I mean not the axis logged but the values logged themselves. So like Log(data) Vs Log(counts) and the axes themselves shouldn't be logged. Thanks for the help. In above fig, I don't want logged y-axis but the values themselves to be logged if that can be done

Upvotes: 1

Views: 1118

Answers (1)

acrider
acrider

Reputation: 406

I'm not sure you can do this with the hist() function itself, but you can recreate it easily with a barchart instead.

import numpy as np
import matplotlib.pyplot as plt

data = np.random.normal(loc=1000,scale=20,size=2000)
n, bins, patches = plt.hist(np.log10(data),log=True)

# Extract the midpoints and widths of each bin.
bin_starts = bins[0:bins.size-1]
bin_widths = bins[1:bins.size] - bins[0:bins.size-1]

# Clear the histogram plot and replace it with a bar plot.
plt.clf()
plt.bar(bin_starts,np.log10(n),bin_widths)
plt.xlabel('Log(data)')
plt.ylabel('Log(Counts)')
plt.show()

Bar Chart

Upvotes: 2

Related Questions