【发布时间】:2021-12-27 08:34:08
【问题描述】:
我有 5 个频率不同的时间序列(pandas.core.series.Series 类),我想制作这个图表:
desired chart
df1 (daily data, only working days)
Date exchange_rate_daily
2013-01-02 25.225
2013-01-03 25.26
2013-01-04 25.35
2013-01-07 25.535
2013-01-08 25.58
2013-01-09 25.53
2013-01-10 25.63
2013-01-11 25.615
2013-01-14 25.615
2013-01-15 25.61
2013-01-16 25.58
2013-01-17 25.54
2013-01-18 25.63
2013-01-21 25.625
df2 (monthly data for 2 time series)
Date intervention_monthly client_monthly
2013-01-01 0.6 0.67273
2013-02-01 0.2 0.04
2013-03-01 0.4 0.17443
2013-04-01 0.3 0.53883
2013-05-01 0.7 -0.0647
2013-06-01 0.2 0.20103
2013-07-01 0.0 0.22846
2013-08-01 0.0 -0.2611
2013-09-01 0.0 0.16002
2013-10-01 0.0 -0.10967
2013-11-01 7.4 -0.31122
折线图:
X 轴:date_exchangerate_daily(大小 = 2198,每日日期,即2013-01-02 00:00:00,但只有工作日 - 没有周末,没有节假日)
Y轴(左):`exchangerate_daily(大小=2198,每日汇率)
我的折线图代码运行良好。见下文:
fig, ax1 = plt.subplots(figsize=(10,6))
ax1.plot(date_exchangerate_daily, exchangerate_daily, color="k", label="Exchange rate (daily)", linewidth=2)
ax1.legend(loc = "upper left")
分组条形图:
X 轴:date_operations_monthly(大小 = 107,每月日期,例如2013-01-01 00:00:00)
Y 轴(右):intervention_monthly(大小 = 107,每月数据)
Y 轴(右):client_monthly(大小 = 107,月度数据)
x = np.arange(len(date_operations_monthly))
width = 0.1
ax2 = ax1.twinx()
rects1 = ax2.bar(x - width/2, intervention_monthly, width, label='Intervention (monthly)')
rects2 = ax2.bar(x + width/2, client_monthly, width, label='Client operation (monthly)')
ax2.set_xticks(x)
ax2.set_xticklabels(date_operations_monthly2, rotation = 70)
ax2.legend(loc = "upper right")
我得到了这张图表: chart
如果我单独绘制图表,我会得到正确的图表。因此,将这两个图表组合在一起存在问题。我想问题是数据频率不同,然后我想在 X 轴上绘制的日期系列(每天 x 每月日期)的大小不同。请问有什么解决办法吗?
非常感谢。
【问题讨论】:
-
欢迎来到这里!请阅读此stackoverflow.com/questions/20109391/… 并编写一个可重现的最小示例
标签: python pandas matplotlib bar-chart linechart