Home > Enterprise >  How to plot a log-log histogram in matplotlib
How to plot a log-log histogram in matplotlib

Time:11-24

I'm wondering how to have a log-log plot for visualizing the frequency of elements in a list, for example:

my_list=[1,2,3,3,3,4,1,2,5,2,10,4,5,3,3,3,2,1]

I plotted data using a histogram:

plt.hist(my_list, label='Frequency Distribution of matches')
plt.legend()
plt.ylabel('Frequency')

But it would be better to visualize it as log-log.

CodePudding user response:

If you want equal-width bars, define bins as 10 ** edges:

start, stop = np.log10(min(my_list)), np.log10(max(my_list))
bins = 10 ** np.linspace(start, stop, 10)

plt.hist(my_list, log=True, bins=bins)
ax.set_xscale('log')

To get a log-log line plot of the frequencies, use

  • Related