绘制多个不同尺度的时间序列

2024-10-08 18:23:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图随着时间的推移绘制4个不同的数据帧,以突出它们之间可能的关系。你知道吗

我遇到了几个困难:

  1. 不同尺度
  2. 相同的值彼此重叠(IOR和IOER曲线)
  3. 曲线有很大的“点”,使它们无法读取
  4. 无法使用移动条形图x值测向指数+0.1,因为我得到一个错误

关于第2点,尝试以这种方式在df ior和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

非常感谢您的帮助!你知道吗


Tags: dfgetdateindexvaluelslabelcolor

热门问题