我试图随时间绘制4个不同的数据框,以突出显示它们之间的可能关系。
我遇到了几个困难:
关于第2点,尝试以这种方式在df IORR和IOER之间移动小节:
p1 = ax1.bar(df_ioer.index + 0.1, df_ioer.Value, ls='dashed', label='IOER', color='g')
ax1.xaxis_date()
我收到此错误:
TypeError: unsupported operand type(s) for +: 'DatetimeIndex' and 'float'
总的来说有点太多了。 有人可以为这个问题提供一些指导以获取数据的直观表示吗?
代码如下:
import quandl
from cycler import cycler
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt
quandl.ApiConfig.api_key = "Get Free Key From Quandl.com"
df_dff = quandl.get("FRED/DFF")
df_iorr = quandl.get("FRED/IORR")
df_ioer = quandl.get("FRED/IOER")
df_gdp = quandl.get("FRED/GDP")
df_dff = df_dff[df_dff.index >= df_iorr.index.min()]
df_iorr = df_iorr[df_iorr.index >= df_iorr.index.min()]
df_ioer = df_ioer[df_ioer.index >= df_iorr.index.min()]
df_gdp = df_gdp[df_gdp.index >= df_iorr.index.min()]
# https://matplotlib.org/gallery/ticks_and_spines/multiple_yaxis_with_spines.html
plt.rc('axes', prop_cycle=(cycler('color', ['r', 'c', 'm', 'y', 'k', 'b', 'g', 'r', 'c', 'm'])))
def make_patch_spines_invisible(ax):
ax.set_frame_on(True)
ax.patch.set_visible(False)
for sp in ax.spines.values():
sp.set_visible(False)
fig, ax0 = plt.subplots()
#p0, = ax0.plot_date(df_iorr.index, df_iorr.Value, ls='dashed', tz=None, xdate=True, ydate=False, label='IORR', color='r')
#ax0.yaxis.label.set_color(p0.get_color())
p0 = ax0.bar(df_iorr.index, df_iorr.Value, ls='dashed', label='IORR', color='r')
ax0.xaxis_date( tz=None)
ax1 = ax0.twinx()
#p1, = ax0.plot_date(df_ioer.index, df_ioer.Value, ls='dashed', tz=None, xdate=True, ydate=False, label='IOER', color='g')
#ax1.yaxis.label.set_color(p1.get_color())
p1 = ax1.bar(df_ioer.index, df_ioer.Value, ls='dashed', label='IOER', color='g')
ax1.xaxis_date( tz=None)
ax2 = ax0.twinx()
p2, = ax0.plot_date(df_dff.index, df_dff.Value, ls='solid', tz=None, xdate=True, ydate=False, label='DFF', color='b')
ax2.spines["right"].set_position(("axes", 1.2))
make_patch_spines_invisible(ax2)
ax2.spines["right"].set_visible(True)
ax3 = ax0.twinx()
p3, = ax3.plot_date(df_gdp.index, df_gdp.Value, ls='solid', tz=None, xdate=True, ydate=False, label='GDP', color='y')
lines = [p0, p1, p2, p3]
ax0.legend(lines, [l.get_label() for l in lines])
plt.show()
任何帮助都将不胜感激!