2

Consider the y ticks in this small example:

import matplotlib.pyplot as plt

plt.plot([200,400,600,800])
plt.ticklabel_format(axis='y',style='sci',scilimits=(0,0))
plt.show()

Resulting plot: tack.imgur.com/Gnl6F.png

I would like the y tick labels to have a fixed format with one decimal, i.e., 1.0 instead of 1, etc. How can I do that while keeping the exponent for the scientific notation on top of the plot?

All solutions I've found so far (most using FormatStrFormatter instead of ScalarFormatter) make the exponent repeat on all tick labels, like 1.0e2, 2.0e2, etc., which is not what I want.

Thank you in advance.

RPT
  • 728
  • 1
  • 10
  • 29

1 Answers1

2

I've just found a solution. Here it goes:

import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

class ScalarFormatterForceFormat(ScalarFormatter):
    def _set_format(self,vmin,vmax):  # Override function that finds format to use.
        self.format = "%1.1f"  # Give format here

fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.plot([200,400,600,800])

yfmt = ScalarFormatterForceFormat()
yfmt.set_powerlimits((0,0))
ax.yaxis.set_major_formatter(yfmt)            

plt.show()

It was based on this answer on a different thread.