简体   繁体   中英

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

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. That cannot work. The trick is to limit the formatting to the axis that actually has numbers ( axis='y' ).

The second issue is that offset refers to the offset to 0 that is not needed, since is similar to all data. What you want is style='plain' . See ticklabel_format documentation .


I couldn't recreate the pd.Series call in my setup, so I use a plain matplotlib.bar call with some sample data. The output should be similar.

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° = 0°, so the rotation is superflous and can be removed.

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