Reputation: 15863
I'm trying to create a plot with a log y-axis and integer tick labels, but at best I'm getting inconsistent decimals.
The simplest approach gives scientific notation:
import matplotlib as mpl
import matplotlib.pyplot as plt
vals = [10, 20, 30]
plt.scatter(vals, vals)
ax = plt.gca()
ax.set_yscale('log')
Setting the axis's major formatter to scalars still yields some scientific notation:
ax.yaxis.set_major_formatter(mpl.ticker.ScalarFormatter())
And setting the minor formatter on top of that removes scientific notation but leaves inconsistent decimals:
ax.yaxis.set_minor_formatter(mpl.ticker.ScalarFormatter())
How can I get all integer tick labels?
Upvotes: 3
Views: 578
Reputation: 339775
As an example, you could use a StrMethodFormatter
with python's g
format:
fmt = mpl.ticker.StrMethodFormatter("{x:g}")
ax.yaxis.set_major_formatter(fmt)
ax.yaxis.set_minor_formatter(fmt)
Upvotes: 2