我在名为GRADES
的列中有一系列值Grades count
A 38616
B 658
G 16041
P 7590
C 33
我想在matplot中的my_data数据集中可视化列GRADE。使用以下代码获取空白子图:
fig, ax=plt.subplots()
ax.set(xlabel="Grade", ylabel="Count", title="Grade Distribution")
ax.bar('GRADE', align='center', data=my_data)
plt.show()
答案 0 :(得分:2)
您正在混合绘图功能的呼号。
假设您的数据位于pandas数据帧中。
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({"Grades" : list("ABGPC"),
"count" : [38616,658,16041,7590,33]})
df.plot(x ="Grades", y="count", kind="bar")
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({"Grades" : list("ABGPC"),
"count" : [38616,658,16041,7590,33]})
fig, ax=plt.subplots()
ax.set(xlabel="Grade", ylabel="Count")
ax.bar(df["Grades"], df["count"])
plt.show()
barplot
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({"Grades" : list("ABGPC"),
"count" : [38616,658,16041,7590,33]})
ax = sns.barplot(x="Grades", y="count", data=df)
plt.show()
答案 1 :(得分:1)
使用条形函数的正确方法是here。您应该将数据作为x
和y
传递,并且成绩必须作为tick_lable
传递
grades = ['A', 'B', 'C']
x = [1, 2, 3]
y = [5, 7, 3]
fig, ax = plt.subplots()
ax.set(xlabel="Grade", ylabel="Count", title="Grade Distribution")
ax.bar(x, y, align='center', tick_label=grades)
plt.show()