1

My question is related to this post, however the solutions there aren't working for me. Below is what I have:

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt

# Create example df
df = pd.DataFrame({
    'date': ['2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01'],
    'Actual': [10250000000, 10350000000, 10400000000, 10380000000],
    'Forecast': [9000000000, 10315000000, 10410000000, 10400000000]
})

#Plot df
plt.rcParams["figure.figsize"] = (14, 8)
fig = plt.figure()
ax  = fig.add_subplot(111)
ax.plot(df.date, df['Actual'], c='black', linewidth=3.0)
ax.plot(df.date, df['Forecast'], c='blue')
plt.show()

Plot of df As you can see, the y-axis has a scale of 1e10. I want this to instead be 1e9. I tried the following solutions from the post I linked to, but they did not work:

plt.rcParams["figure.figsize"] = (14, 8)
plt.rcParams['axes.formatter.useoffset'] = False
fig = plt.figure()
ax  = fig.add_subplot(111)
ax.plot(df.date, df['Actual'], c='black', linewidth=3.0)
ax.plot(df.date, df['Forecast'], c='blue')
plt.show()

and

plt.rcParams["figure.figsize"] = (14, 8)    
fig = plt.figure()
ax  = fig.add_subplot(111)
ax.plot(df.date, df['Actual'], c='black', linewidth=3.0)
ax.plot(df.date, df['Forecast'], c='blue')
y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
ax.yaxis.set_major_formatter(y_formatter)
plt.show()
Gaurav Bansal
  • 5,221
  • 14
  • 45
  • 91
  • I did in fact find what you were looking for in the [post you linked to](https://stackoverflow.com/q/3677368/425458). For whatever reason the actually correct answer is the [third ranked one](https://stackoverflow.com/a/3679918/425458). – tel Apr 11 '18 at 14:49

1 Answers1

0

I'm not going to claim I came up with this one, but:

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter, FormatStrFormatter

class FixedOrderFormatter(ScalarFormatter):
    """Formats axis ticks using scientific notation with a constant order of 
    magnitude"""
    def __init__(self, order_of_mag=0, useOffset=True, useMathText=False):
        self._order_of_mag = order_of_mag
        ScalarFormatter.__init__(self, useOffset=useOffset, 
                                 useMathText=useMathText)
    def _set_orderOfMagnitude(self, range):
        """Over-riding this to avoid having orderOfMagnitude reset elsewhere"""
        self.orderOfMagnitude = self._order_of_mag

# Create example df
df = pd.DataFrame({
    'date': ['2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01'],
    'Actual': [10250000000, 10350000000, 10400000000, 10380000000],
    'Forecast': [9000000000, 10315000000, 10410000000, 10400000000]
})

#Plot df
plt.rcParams["figure.figsize"] = (14, 8)
fig = plt.figure()
ax  = fig.add_subplot(111)
ax.plot(df.date, df['Actual'], c='black', label='Actual', linewidth=3.0)
ax.plot(df.date, df['Forecast'], c='blue', label='Forecast')
leg = plt.legend()

# set the desired exponent using the FixedOrderFormatter class defined above
ax.yaxis.set_major_formatter(FixedOrderFormatter(9))

plt.show()

output:

enter image description here

tel
  • 13,005
  • 2
  • 44
  • 62
  • Somehow I'm getting this error: `name 'FixedOrderFormatter' is not defined`. – Gaurav Bansal Apr 11 '18 at 14:48
  • @GauravBansal Splitting the code into two blocks was a little confusing I suppose. I've combined it all together, so you shouldn't run into that problem again in any case – tel Apr 11 '18 at 14:50