1

The following produces a plot in which y ticks are -1, -0.5, 0, 0.5, 1 and their scaling is plotted elsewhere, how do I force matplotlib to plot the tics in-place as -1e-9, -0.5-e9... so I don't have to search other parts of the plot for potential corrections to tick values?

import matplotlib.pyplot as plot
plot.plot([0, 1e-9, 0, -1e-9])
plot.show()

Google led me to plot.ticklabel_format(useOffset = False) but unfortunately that doesn't have a useScaling parameter.

cel
  • 30,017
  • 18
  • 97
  • 117
user3493721
  • 185
  • 6

1 Answers1

1

One way to do this is to get the y-axis tick values, format them as you like, and then add them back to the plot as y-axis tick labels.

import matplotlib.pyplot as plt
plt.plot([0, 1e-9, 0, -1e-9])

# pull out values for y-axis ticks
yticks = plt.gca().get_yticks()

# format new labels
new_yticklabels = ['{:0=4.2f}e-9'.format(tick/1e-9) for tick in yticks]

# set new labels
plt.gca().set_yticklabels(new_yticklabels)

plt.show()

Output

This may not be the best way, but it works. There is probably a way to do this using ticker formatters such as FuncFormatter.

This answer was helpful for getting the scientific notation formatting correct.

Community
  • 1
  • 1
jonchar
  • 6,183
  • 1
  • 14
  • 19
  • I used `{:0=.2e}'.format(ytick)` instead but otherwise this worked. I also asked the mailing list but didn't get an answer so maybe this is the only way to do it. – user3493721 Feb 14 '16 at 15:11