使用python对给定数据进行数据可视化
我想为以下数据绘制图表 我在excel中获得此输出,但当我尝试使用pandas时使用python对给定数据进行数据可视化,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,我想为以下数据绘制图表 我在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.
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();
我得到的电流输出
预期产量
您需要重新设置数据的范围。Pandas area plot从列中获取值,您需要为每行打印这些值。对于此用途
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()
输出: