折线图+不同频率的分组条形图

Line chart + grouped bar chart with different frequency

我有 5 个不同频率的时间序列 (class 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 每月日期)不同。请问有解决办法吗?

非常感谢。

由于没有可用的数据,我使用了股价数据,并使用收盘价、交易量和另一个条形图的 1/2 的交易量创建了一个条形图。 matplotlib时间序列是基于公历的,所以需要进行转换。查看更多 details

import pandas as pd
import numpy as np
import io

data = '''
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
'''

data1 = '''
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
'''
df1 = pd.read_csv(io.StringIO(data), delim_whitespace=True)
df2 = pd.read_csv(io.StringIO(data1), delim_whitespace=True)
df1['Date'] = pd.to_datetime(df1['Date'])
df2['Date'] = pd.to_datetime(df2['Date'])

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

fig, ax1 = plt.subplots(figsize=(12,6))
ax1.plot(mdates.date2num(df1['Date']), df1['exchange_rate_daily'], color="k", label="Exchange rate (daily)", linewidth=2)
ax1.legend(loc = "upper left")

#x = np.arange(len(df2))
width = 0.5
ax2 = ax1.twinx()
rects1 = ax2.bar(mdates.date2num(df2['Date']) - 1, df2['intervention_monthly'], width, label='Intervention (monthly)')
rects2 = ax2.bar(mdates.date2num(df2['Date']) + 1, df2['client_monthly'], width, label='Client operation (monthly)')
all_dates = sorted(df1['Date'][::5].tolist()+df2['Date'].tolist())
all_dates = mdates.date2num(all_dates)
ax2.set_xticks(all_dates)
ax2.set_xticklabels([mdates.num2date(d).strftime('%Y-%m-%d') for d in all_dates])
ax2.legend(loc = "upper right") 
ax1.tick_params(axis='x', labelrotation=90)

plt.show()