我有2个问题
第一 当我收到“ Empty'DataFrame':没有要绘制的数字数据”时,有没有办法绘制(未堆叠)此数据框?
df=pd.DataFrame({'midterm':['A','B','B','D'],'Final':['C','A','D','B']}, index=['math', 'sport', 'History', 'Physics'])
第二个问题: 我像这样手动绘制数据框的数据
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df=pd.DataFrame({'midterm':['A','B','B'],'Final':['C','A','D']}, index=['math', 'sport', 'History'])
fig, ax = plt.subplots()
index = np.asarray([1,10])
width=0.5
plt.bar(index, df.iloc[0,:], width,label='Math')
plt.bar(index+width, df.iloc[1,:], width, label='Sport')
plt.bar(index+2*width, df.iloc[2,:], width, label='History')
xticks=['midterm','final']
plt.xticks=(index,xticks)
plt.legend()
plt.show()
What the code produces is here
这有两个问题, -首先,不订购A,B,C,D -其次,y轴从0,0点开始,这使得该图中的C级钢筋根本不可见
答案 0 :(得分:0)
我将回答您的第二个问题。您应该将问题限制为单个查询。我将等级映射到整数,然后设置y-ticks。为此,我定义了一个映射字典和一个函数mapping
,该函数采用字母等级并将其转换为条形图高度的整数值。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df=pd.DataFrame({'midterm':['A','B','B'],'Final':['C','A','D']}, index=['math', 'sport', 'History'])
map_dict = {'A': 1, 'B': 2, 'C': 3, 'D':4}
def mapping(keys):
values = [map_dict[i] for i in keys]
return values
fig, ax = plt.subplots()
index = np.asarray([1,10])
width=0.5
plt.bar(index, mapping(df.iloc[0,:]), width,label='Math')
plt.bar(index+width, mapping(df.iloc[1,:]), width, label='Sport')
plt.bar(index-width, mapping(df.iloc[2,:]), width, label='History')
xticks=['midterm','final']
ax.set_yticks(range(1, 5))
ax.set_yticklabels(map_dict.keys())
plt.legend()