Search code examples
pythonnumpymatplotlibaxis-labels

How to input exponentiation in label using set_yticklabels in matplotlib


Hi I want to set tick labels as multiplication of some numbers by 6th power of 10. The problem is that I don't know how to make matplotlib to show it nicely. I do following:

ax.set_yticks([1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000])
ax.set_yticklabels([ '1.0*10^6', '1.2*10^6', '1.4*10^6', '1.6*10^6', '1.8*10^6', '2.0*10^6', '2.2*10^6', '2.4*10^6' ], rotation=0, ha='center', va='top')

The effect of above is not what I want to see. I got:

enter image description here

But I would like to have following:

enter image description here

How can I archieve that?


Solution

  • You need to use math notation. This works as in Latex by enclosing you math expressions with $. It is explained in some detail here.

    For your case you could write you strings as:

    ax.set_yticklabels([ '$1.0 \cdot 10^6$', '$1.2 \cdot 10^6$',...], rotation=0, ha='center', va='top')