简体   繁体   中英

Awkward scientific notation on the axis in matplotlib

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.

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

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM