Max Ghenis
Max Ghenis

Reputation: 15863

Inconsistent decimals in tick labels when using a log axis

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')

First graph

Setting the axis's major formatter to scalars still yields some scientific notation:

ax.yaxis.set_major_formatter(mpl.ticker.ScalarFormatter())

Second graph

And setting the minor formatter on top of that removes scientific notation but leaves inconsistent decimals:

ax.yaxis.set_minor_formatter(mpl.ticker.ScalarFormatter())

Graph 3

How can I get all integer tick labels?

Upvotes: 3

Views: 578

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

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)

enter image description here

Upvotes: 2

Related Questions