我的问题与此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()
答案 0 :(得分:0)
我不是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()
输出: