[英]Data Visualization for the given data using python
I want to draw graph for the following Data我想为以下数据绘制图形
I get this output in excel but when I try to do it using pandas我在 excel 中得到了这个输出,但是当我尝试使用 Pandas 来做时
SOF=pd.read_clipboard()
'''
Index One Two Three Four Five Six Seven
A 0.137931034 0.142857143 0.184210526 0.178571429 0.157894737 0.085714286 0.887179155
B 0.068965517 0.095238095 0.052631579 0.107142857 0.105263158 0.142857143 0.572098349
C 0.103448276 0.047619048 0.078947368 0.071428571 0.026315789 0.2 0.527759053
D 0.172413793 0.166666667 0.210526316 0.25 0.236842105 0.085714286 1.122163167
E 0.172413793 0.142857143 0.236842105 0 0.078947368 0.142857143 0.773917553
F 0.24137931 0.142857143 0.026315789 0.214285714 0.157894737 0.028571429 0.811304122
G 0.068965517 0.19047619 0.052631579 0.142857143 0.131578947 0.057142857 0.643652234
H 0.034482759 0.071428571 0.157894737 0.035714286 0.105263158 0.257142857 0.661926368
'''
SOF.plot.area();
You need to rearange you data.您需要重新排列数据。 Pandas area plot get values from columns and you need them plot for each row.
熊猫面积图从列中获取值,您需要为每一行绘制它们。 For this use
matplotlib
:为此使用
matplotlib
:
import pandas as pd
from io import StringIO
from matplotlib import pyplot as plt
data = StringIO('''
Index One Two Three Four Five Six Seven
A 0.137931034 0.142857143 0.184210526 0.178571429 0.157894737 0.085714286 0.887179155
B 0.068965517 0.095238095 0.052631579 0.107142857 0.105263158 0.142857143 0.572098349
C 0.103448276 0.047619048 0.078947368 0.071428571 0.026315789 0.2 0.527759053
D 0.172413793 0.166666667 0.210526316 0.25 0.236842105 0.085714286 1.122163167
E 0.172413793 0.142857143 0.236842105 0 0.078947368 0.142857143 0.773917553
F 0.24137931 0.142857143 0.026315789 0.214285714 0.157894737 0.028571429 0.811304122
G 0.068965517 0.19047619 0.052631579 0.142857143 0.131578947 0.057142857 0.643652234
H 0.034482759 0.071428571 0.157894737 0.035714286 0.105263158 0.257142857 0.661926368
''')
df = pd.read_csv(data, sep=' ', engine='python')
names = list(df['Index'])
df = df.drop(['Index'], axis=1)
# plot data
fig, ax = plt.subplots()
count = 0
for name in names:
a = df.iloc[count]
labels = ['0'] + list(df.columns)
x = range(7)
ax.fill_between(x, a)
ax.set_xticklabels(labels)
ax.plot(x, a, '-', label=name)
count += 1
# shrink plot box
box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8])
# plot legend
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), shadow=False, ncol=2)
plt.show()
Output:
输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.