【发布时间】:2020-11-08 17:54:36
【问题描述】:
所以我一直在尝试用 Python 中的 DF 制作水平条形图。我成功了,但是 X 轴的值顺序不对。
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
plt.rcdefaults()
fig, ax = plt.subplots()
plt.xlim(0, 14)
# data
people = (a.Ethnicity)
y_pos = np.arange(len(people))
performance = a.Value
ax.barh(y_pos, performance, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('Value')
ax.set_title('Unemployment between different ethnic groups in 2004')
plt.show()
这段代码给了我下图: Graph from Python
但我希望它是这样的:This graph 或者是这样的:Graph from Excel
这是变量a:
a = df.loc[(df.Time == 2018) & (df.Region == "All") & (df.Age == "All") & (df.Sex == "All"), ["Ethnicity","Value"]]
print(a)
Ethnicity Value
32772 All 4.2
32952 Asian 6.2
33132 Asian Other 6.1
33312 Black 8.8
33492 Indian 4.3
33672 Mixed 7
33852 Other 7.5
34032 Other than White 7.1
34212 Pakistani and Bangladeshi 8.4
34392 Unknown 4.1
34572 White 3.7
34752 White British 3.8
34932 White Other 3.4
【问题讨论】:
标签: python dataframe csv graph bar-chart