【发布时间】:2018-06-28 23:28:17
【问题描述】:
我目前正在从 pandas df 手动创建 bar chart。有没有更有效的方法可以直接从df 读取它?
import pandas as pd
import matplotlib.pyplot as plt
d = ({
'A' : ['1','1','1','2','2','2','3','3','3','3'],
'B' : ['A','B','C','A','B','C','D','A','B','C'],
'C' : ['John','Carl','Carl','John','Lily','John','Lily','John','Carl','Carl'],
})
df = pd.DataFrame(data=d)
fig,ax = plt.subplots(figsize = (3,3))
Ay = [3]
Ax = [1]
By = [2,1]
Bx = [7,12]
Cy = [1,2]
Cx = [3,8]
Dy = [1]
Dx = [14]
plt.bar(Ax,Ay, label = 'A')
plt.bar(Bx,By, label = 'B')
plt.bar(Cx,Cy, label = 'C')
plt.bar(Dx,Dy, label = 'D')
plt.legend(loc = 'upper right')
plt.show()
如果我手动添加 xticks 以显示预期输出的人:
【问题讨论】:
标签: python pandas matplotlib bar-chart