简体   繁体   中英

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

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();

Current output I get电流输出

Expected output预期产出

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 :

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM