简体   繁体   English

试图摆脱科学记数法时如何修复属性错误? Python

[英]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.

相关问题 如何在 Python 中摆脱科学记数法并转换为浮点数? - How do I get rid of scientific notation and covert to a floating number in Python? 科学计数法的Python模错误 - Error with Python modulo on scientific notation 如何摆脱因将科学记数法转换为精确值而产生的 json 字符串中的引号 - How to get rid of quotes in a json string resulting from converting scientific notation to exact value 尝试使用 Python 获取 Excel 差异时如何修复 Key 错误 - How to fix a Key error when trying to get an Excel diff with Python 如何消除 Python 中的 class 的属性错误? - How do i get rid of attribute error for my class in Python? 是否有一个python模块将值和错误转换为科学记数法? - Is there a python module that convert a value and an error to a scientific notation? 运行fuzzywuzzy时如何摆脱属性错误? - How do I get rid of attribute Error when running fuzzywuzzy? "尝试抓取网站时如何摆脱错误?" - How do I get rid of an error when trying to scrape a site? 如何在 matplotlib 上获得科学计数法的乘数字符串 - How to get multiplier string of scientific notation on matplotlib 当min(list)为科学计数法时如何修复list.index(min(list)) - How to fix list.index(min(list)) when min(list) is in scientific notation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM