这是我的数据集的一个示例,其中x标签为每月一次,而y值为双周一次:
month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
temp_1 = [35, 45, 55, 65, 75, 85, 95, 100, 85, 65, 45, 35, 55, 65, 75, 85, 95, 100, 35, 45, 55, 65, 75, 85,]
temp_2 = [30, 35, 65, 55, 70, 85, 65, 85, 85, 75, 65, 75, 65, 85, 85, 75, 65, 75, 85, 85, 75, 65, 75, 65,]
kw_1 = [5, 23, 5 , 6, 6, 10, 3, 0 , 1, 4, 5, 23, 5 , 6, 3, 1, 6, 10, 3, 0, 0 , 1, 4, 8]
kw_2 = [6, 6, 10, 3, 0 , 1, 4, 6, 10, 3, 0, 6, 10, 3, 0, 5, 23, 5 , 6, 5, 23, 5 , 1, 25 ]
# set peak
peak_1 = max(kw_1)
peak_index_1 = kw_1.index(peak_1)
peak_2 = max(kw_2)
peak_index_2 = kw_2.index(peak_2)
答案 0 :(得分:1)
所提供的数据与所需的输出时间序列不同,但我绘制了x轴为一个月,左侧为以KW堆叠的条形图,右侧为以温度为线形的线形图
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']*2
temp_1 = [35, 45, 55, 65, 75, 85, 95, 100, 85, 65, 45, 35, 55, 65, 75, 85, 95, 100, 35, 45, 55, 65, 75, 85,]
temp_2 = [30, 35, 65, 55, 70, 85, 65, 85, 85, 75, 65, 75, 65, 85, 85, 75, 65, 75, 85, 85, 75, 65, 75, 65,]
kw_1 = [5, 23, 5 , 6, 6, 10, 3, 0 , 1, 4, 5, 23, 5 , 6, 3, 1, 6, 10, 3, 0, 0 , 1, 4, 8]
kw_2 = [6, 6, 10, 3, 0 , 1, 4, 6, 10, 3, 0, 6, 10, 3, 0, 5, 23, 5 , 6, 5, 23, 5 , 1, 25 ]
df1 = pd.DataFrame({'month':month,'kw_1':kw_1,'kw_2':kw_2})
df2 = pd.DataFrame({'month':month,'temp_1':temp_1,'temp_2':temp_2})
df1 = df1.groupby('month', sort=False).sum().reset_index()
df2 = df2.groupby('month', sort=False).sum().reset_index()
fig, ax1 = plt.subplots(figsize=(12,6))
sns.set()
df1.set_index('month').plot(kind='bar', stacked=True, grid=False, legend=False, ax=ax1)
# sns.barplot(data=df1, x='month', y=['kw_1','kw_2'], alpha=0.5, ax=ax1)
ax2 = ax1.twinx()
sns.lineplot(data=df2, marker='o', sort = False, legend='auto', ax=ax2)
handles, labels = ax1.get_legend_handles_labels()
handles1, labels1 = ax2.get_legend_handles_labels()
ax2.legend(handles+handles1, labels+labels1, loc='upper left', bbox_to_anchor=(1.05, 1), frameon=False)
plt.show()