我有从 Excel 电子表格中读取的数据。数据对 S1 到 S6 六种情景中的每一种都有许多观察结果。当我将数据读入我的数据框 df 时,它看起来如下:
Scenario LMP
0 S1 -21.454544
1 S1 -20.778094
2 S1 -20.027689
3 S1 -19.747170
4 S1 -20.814405
5 S1 -21.955406
6 S1 -23.018960
...
12258 S6 -34.089906
12259 S6 -34.222814
12260 S6 -26.712010
12261 S6 -24.555973
12262 S6 -23.062616
12263 S6 -20.488411
我想为六个场景中的每一个创建一个具有不同小提琴的小提琴图。我是 Pandas 和数据框的新手,尽管过去一天进行了大量研究/测试,但我无法找到一种优雅的方式来将一些引用传递给我的数据框(将其拆分为每个场景的不同系列) 将在 axes.violinplot() 语句中工作。例如,我尝试了以下方法,但不起作用。我在 axes.violinplot 语句中收到“ValueError:无法将大小为 1752 的序列复制到维度为 2 的数组轴”。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# load data into a dataframe
df = pd.read_excel('Modeling analysis charts.xlsx',
sheetname='lmps',
parse_cols=[7,12],
skiprows=0,
header=1)
fontsize = 10
fig, axes = plt.subplots()
axes.violinplot(dataset = [[df.loc[df.Scenario == 'S1']],
[df.loc[df.Scenario == 'S2']],
[df.loc[df.Scenario == 'S3']],
[df.loc[df.Scenario == 'S4']],
[df.loc[df.Scenario == 'S5']],
[df.loc[df.Scenario == 'S6']]
]
)
axes.set_title('Day Ahead Market')
axes.yaxis.grid(True)
axes.set_xlabel('Scenario')
axes.set_ylabel('LMP ($/MWh)')
plt.show()