Gamer
Gamer

Reputation: 45

How to fix attribute error when trying to get rid of scientific notation? Python

enter image description here

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

Upvotes: 0

Views: 768

Answers (1)

Cpt.Hook
Cpt.Hook

Reputation: 618

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()

Outcome


Final remark: Why do you rotate by 360°? 360° = 0°, so the rotation is superflous and can be removed.

Upvotes: 2

Related Questions