我是Python的初学者,正在使用Matplotlib和Pandas创建堆积区域图(例如https://python-graph-gallery.com/251-stacked-area-chart-with-seaborn-style/)。不幸的是,它不起作用。我有以下包含数据的文件:
现在,我需要存储每个列中的值,为此,我使用以下命令:
ts_1 = wind_data.Building_1;
ts_2 = wind_data.Building_2;
ts_3 = wind_data.Building_3;
ts_4 = wind_data.Building_4;
ts_5 = wind_data.Building_5
y= [ts_1, ts_2, ts_3, ts_4, ts_5];
如果我现在在上面示例链接的命令中使用此命令:
# library
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Data
x=range(1,6)
# Plot
plt.stackplot(x,y, labels=['A','B','C'])
plt.legend(loc='upper left')
plt.show()
我收到一条错误消息。我认为问题在于,当我读取变量ts_1中的单列时,我并不仅获得值,而且还获得了索引,如下图所示:
我希望如果我只能将值存储在没有索引的列的变量中,则可以创建堆栈图。我该怎么办?
这是样本数据集:
Building_1 Building_2 Building_3 Building_4 Building_5
7.04 7.04 7.04 7.04 7.04
6.36 6.36 6.36 6.36 6.36
6.4 6.4 6.4 6.4 6.4
6.1 6.1 6.1 6.1 6.1
5.88 5.88 5.88 5.88 5.88
6.18 6.18 6.18 6.18 6.18
6.16 6.16 6.16 6.16 6.16
5.82 5.82 5.82 5.82 5.82
5.28 5.28 5.28 5.28 5.28
4.82 4.82 4.82 4.82 4.82
4.18 4.18 4.18 4.18 4.18
4.02 4.02 4.02 4.02 4.02
4.08 4.08 4.08 4.08 4.08
4.24 4.24 4.24 4.24 4.24
6.24 6.24 6.24 6.24 6.24
8.44 8.44 8.44 8.44 8.44
8.72 8.72 8.72 8.72 8.72
8.06 8.06 8.06 8.06 8.06
7.16 7.16 7.16 7.16 7.16
6.52 6.52 6.52 6.52 6.52
7.16 7.16 7.16 7.16 7.16
7.88 7.88 7.88 7.88 7.88
8.44 8.44 8.44 8.44 8.44
8.56 8.56 8.56 8.56 8.56
这是执行此操作的简单方法:
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# sample data, in your case this is wind_data
df = pd.DataFrame({'A': [1,2,3], 'B':[2,3,4], 'C': [5,6,7], 'D': [6,7,8]})
# from here, replace df with wind_data
x = range(df.shape[1])
y = df.values.tolist()
plt.stackplot(x,y, labels=df.columns)
plt.legend(loc='upper left')
plt.show()
或者,您也可以执行以下操作:
wind_data.plot.area() # or you can also do df.plot.area()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句