Home > Enterprise >  How can I get matplotlib to use equal width ' ' and '-' in number formatter?
How can I get matplotlib to use equal width ' ' and '-' in number formatter?

Time:10-13

Normally matplotlib uses a unicode negative sign for tick labels on negative numbers.

plt.figure()
plt.plot([-10,10],[-10,10])

Note the wide negative signs on both axes if you run this.

Sometimes I want to get an explicit positive sign ' ' in the positive tick labels, so I tried the following:

plt.gca().yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter('% .2f'))

The plus signs appear, but the minus signs shrink to hyphens.

How can I get both plus and minus signs that are the same width using the formatter?

The answer to this similar question requires manually setting the ticks and tick labels, which is suboptimal. enter image description here

  • Related