绘制具有不同比例的多个时间序列

时间:2018-12-14 23:00:28

标签: python matplotlib time-series

我试图随时间绘制4个不同的数据框,以突出显示它们之间的可能关系。

我遇到了几个困难:

  1. 不同尺度
  2. 相同的值相互重叠(IORR和IOER曲线)
  3. 曲线有很大的“要点”,使它们不可读
  4. 当我遇到错误时,无法移动条形图以df.index + 0.1绘制x值

关于第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()

结果看起来像这样,还远远不够。multiple time series with various scales

任何帮助都将不胜感激!

0 个答案:

没有答案