Search code examples
pythonmatplotlibplotlogarithm

Matplotlib:non logarithmic labels when plotting logarithm


I am plotting a histogram, taking the logarithm of the quantity before plotting, (instead of choosing a logarithmic scale for my plot) and in the tick labels on the x axis I obviously get the logarithm. I would like labels to show the scientific notation instead: in other words I would like 10^3 instead of 3, for example.

Is there any way to do that? I saw other questions, like Matplotlib - logarithmic scale, but require non-logarithmic labels, but this is not the same thing, I didn' use ax.set_xscale('log'). Here is a line of my code:

ax[0, 0].hist(np.log10(bh_100[:, 0]),bins=15, ls='dashed', color= 'b',   log=True, label = 'Bh-bh')

Solution

  • You can always use set_xticklabels to set the labels as you require them

    For example, you can do:

    ax.set_xticklabels(['10^{%d}'%i for i in range(5)])
    

    or LaTeX-style:

    ax.set_xticklabels(['$10^{%d}$'%i for i in range(5)])