Awkward scientific notation on the axis in matplotlib

835 Views Asked by At

So I would like to change the way the numbers are presented on the x and y axis. I would either like no powers being represented at all or the powers in each individual tick. Is this possible?

indx = np.where(time > 0.28549) # indexs where i want to zoom in
plt.plot(time[indx], dist[indx], label='Without air resistance')
plt.plot(time[indx], distd[indx], label='With air resistance')
plt.xlabel('Time / s')
plt.ylabel('Distance from $y_0$ / m')
plt.ticklabel_format(style='plain')
plt.title('A closer look at the distance-time graph')
plt.legend()
print(time[-1])

graph

thanks.

1

There are 1 best solutions below

0
On

You can use the tick formatter

from matplotlib.ticker import FormatStrFormatter

time = np.linspace(0.285489, 0.285493, 100)
dist = 2*time
distd = 2*time+0.0000025
indx = np.where(time > 0.28549) # indexs where i want to zoom in
plt.plot(time[indx], dist[indx], label='Without air resistance')
plt.plot(time[indx], distd[indx], label='With air resistance')
plt.xlabel('Time / s')
plt.ylabel('Distance from $y_0$ / m')
plt.ticklabel_format(style='plain')

### Format the tick labels, does nothing to the value.
plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%.7f'))
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%.7f'))

plt.title('A closer look at the distance-time graph')
plt.legend()

enter image description here

As a matter of personal opinion however, I think the matplotlib default looks better because it makes it easier to work out the tick intervals given such a distracting number of decimal places.