简体   繁体   中英

Formatting y-axis tick text plot

The tick text of the y axis in my matplotlib plot has too many decimal digits. How can I format the text to show two decimal digits only. IE I would like to convert 10.39658602 into 10.40

I've already tried with:

ax.get_yaxis().set_major_formatter(matplotlib.ticker.FormatStrFormatter('%5.2f'))

but the decimal digits are set to 0 (the previous number becomes 10.00 ). Here is some image. Before:

在此处输入图片说明

After: 在此处输入图片说明

Edit: I'm including also an example of the code to reproduce the problem.

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
lineObjects = plt.plot(epochs, data1, 'rs',
                       epochs, data2, 'b^',
                       epochs, data3, 'gv',
                       epochs, data4, 'ko', ) 
# also %.2f does not work as intended
ax.get_yaxis().set_major_formatter(matplotlib.ticker.FormatStrFormatter('%5.2f'))
plt.show()

I'm using Python 3.6 on Anaconda. data1 , data2 , data3 , data4 are NumPy ndarray s of type str_ , I've already tried to convert them to float , but the results are similar.

使用这种格式。

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

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