corsair
corsair

Reputation: 387

How to plot a cumulative histogram in pyplot with logarithmic xscale only

This plots logarithmic xscale AND yscale. Can't seem to figure out how to plot logarithmic xscale only.

plt.hist(data, bins=10, cumulative=True, log=True)

Upvotes: 2

Views: 7635

Answers (1)

tiago
tiago

Reputation: 23492

You can change the log in the y axis with the following:

plt.gca().set_yscale('linear')

Or press the L key when the figure is in focus.

However, your hist() with log=True does not plot a logarithmic x axis. From the docs:

matplotlib.pyplot.hist(x, bins=10, ...)

bins: Either an integer number of bins or a sequence giving the bins. If bins is an integer, bins + 1 bin edges will be returned, consistent with numpy.histogram() for numpy version >= 1.3, and with the new = True argument in earlier versions. Unequally spaced bins are supported if bins is a sequence.

So if you just set bins=10 they will be equally spaced, which is why when you set the xscale to log they have decreasing widths. To get equally spaced bins in a log xscale you need something like:

plt.hist(x, bins=10**np.linspace(0, 1, 10))

Upvotes: 2

Related Questions