【问题标题】:Change y-axis offset value in matplotlib [duplicate]在matplotlib中更改y轴偏移值[重复]
【发布时间】:2018-04-11 14:35:50
【问题描述】:

我的问题与post 有关,但是那里的解决方案对我不起作用。以下是我所拥有的:

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

如您所见,y 轴的比例为 1e10。我希望它改为 1e9。我从链接到的帖子中尝试了以下解决方案,但它们不起作用:

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

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

【问题讨论】:

标签: python matplotlib


【解决方案1】:

我不是going to claim I came up with this one,而是:

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

输出:

【讨论】:

  • 不知何故我得到了这个错误:name 'FixedOrderFormatter' is not defined.
  • @GauravBansal 我想将代码分成两个块有点令人困惑。我已经把它们结合在一起了,所以无论如何你都不应该再遇到这个问题了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 2018-06-12
  • 2022-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多