[英]How to fix attribute error when trying to get rid of scientific notation? Python
I am trying to have the actual values just written instead of the scientific notation that it currently has.我正在尝试使用刚刚写入的实际值而不是它当前拥有的科学记数法。 Here is my code that I currently have:
这是我目前拥有的代码:
fig = plt.figure(figsize = (10, 5))
pd.Series(y_vals, index=x_vals).nlargest(10).plot.bar(color ='maroon', width = 0.4)
plt.xlabel("Company")
plt.xticks(rotation = 360)
plt.ylabel("Value of Purchases")
plt.title("Top Insider Purchases of the Year")
#plt.ticklabel_format(useOffset=False)
plt.show()
When I use the code that I commented out, I get this error- AttributeError: This method only works with the ScalarFormatter当我使用我注释掉的代码时,我得到这个错误 - AttributeError: This method only works with the ScalarFormatter
I tried to change it to scalar format too but it was saying it is undefined.我也尝试将其更改为标量格式,但它说它未定义。 Thanks in advance
提前致谢
The first issue is that you are trying to format string
labels with a number formatter.第一个问题是您正在尝试使用数字格式化程序来格式化
string
标签。 That cannot work.那行不通。 The trick is to limit the formatting to the axis that actually has numbers (
axis='y'
).诀窍是将格式限制为实际具有数字的轴 (
axis='y'
)。
The second issue is that offset
refers to the offset to 0
that is not needed, since is similar to all data.第二个问题是
offset
指的是不需要的到0
的偏移量,因为它类似于所有数据。 What you want is style='plain'
.你想要的是
style='plain'
。 See ticklabel_format
documentation .请参阅
ticklabel_format
文档。
I couldn't recreate the pd.Series
call in my setup, so I use a plain matplotlib.bar
call with some sample data.我无法在我的设置中重新创建
pd.Series
调用,因此我使用带有一些示例数据的普通matplotlib.bar
调用。 The output should be similar. output 应该是类似的。
import matplotlib.pyplot as plt
companies = ['AKUS', 'UHAL', 'HHC', 'ASPN']
purchases = [3.6e8, 2.45e8, 1.5e8, 1.1e8]
fig = plt.figure(figsize=(10, 5))
plt.bar(companies, purchases)
plt.xlabel("Company")
plt.ylabel("Value of Purchases")
plt.title("Top Insider Purchases of the Year")
plt.ticklabel_format(axis='y', style='plain')
plt.show()
Final remark: Why do you rotate by 360°?结语:为什么要旋转 360°? 360° = 0°, so the rotation is superflous and can be removed.
360° = 0°,因此旋转是多余的,可以删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.