turn off scientific notation for matplotlib

ghz 昨天 ⋅ 3 views

I want to turn off the scientific notation for both graphs on the y-axis. I added ticklabel_format but it does not work. how to let two y-axis both use plain notations?

import pandas as pd
import datetime as dt
import pandas_datareader as web
import matplotlib.pyplot as plt
from matplotlib import style
style.use('dark_background')

prev=3000
end=dt.datetime.today().date()
st=end-pd.to_timedelta(prev,unit='d')

init_jobless=web.DataReader('ICSA','fred',st,end)
cont_jobless=web.DataReader('CCSA','fred',st,end)

fig, axs = plt.subplots(2,sharex=True)
axs[0].plot(init_jobless,label='US Initial Jobless Claims')
axs[0].set_title('US Initial Jobless Claims')
axs[1].plot(cont_jobless)
axs[1].set_title('US Continued Jobless Claims')
axs.ticklabel_format(axis='y',style='plain')
plt.show()
print(init_jobless,cont_jobless)

Answer

To turn off scientific notation for both y-axes in your plot, you need to make sure that the ticklabel_format is applied to both axs[0] and axs[1], not just the general axs. Here's the corrected code:

import pandas as pd
import datetime as dt
import pandas_datareader as web
import matplotlib.pyplot as plt
from matplotlib import style
style.use('dark_background')

prev = 3000
end = dt.datetime.today().date()
st = end - pd.to_timedelta(prev, unit='d')

init_jobless = web.DataReader('ICSA', 'fred', st, end)
cont_jobless = web.DataReader('CCSA', 'fred', st, end)

fig, axs = plt.subplots(2, sharex=True)
axs[0].plot(init_jobless, label='US Initial Jobless Claims')
axs[0].set_title('US Initial Jobless Claims')
axs[1].plot(cont_jobless)
axs[1].set_title('US Continued Jobless Claims')

# Turn off scientific notation for both y-axes
axs[0].ticklabel_format(axis='y', style='plain')
axs[1].ticklabel_format(axis='y', style='plain')

plt.show()
print(init_jobless, cont_jobless)

Explanation:

  • The ticklabel_format method needs to be applied individually to both axes axs[0] and axs[1] to ensure both y-axes use plain notation.
  • The axis='y' argument ensures that the format is applied specifically to the y-axis, and style='plain' disables scientific notation.

This should now display the y-axis values in plain notation (not scientific notation) for both subplots.