简体   繁体   English

格式化 y 轴刻度文本图

[英]Formatting y-axis tick text plot

The tick text of the y axis in my matplotlib plot has too many decimal digits.我的 matplotlib 图中 y 轴的刻度文本有太多十进制数字。 How can I format the text to show two decimal digits only.如何格式化文本以仅显示两位十进制数字。 IE I would like to convert 10.39658602 into 10.40 IE 我想把10.39658602转换成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 ).但十进制数字被设置为 0 (之前的数字变为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.我在 Anaconda 上使用 Python 3.6。 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. data1data2data3data4是NumPy的ndarray S型str_ ,我已经尝试将它们转换为float ,但结果是类似的。

使用这种格式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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