import matplotlib.pyplot as plt
import education
list_of_record = education.get_all_states
virginia_education = education.get_state('Virginia')
enrollment = virginia_education ["enrollment"]
students = enrollment ["students"]
race = students ["race"]
asian = race["asian"]
biracial = race["biracial"]
hispanic = race ["hispanic"]
white = race ["white"]
black = race["black"]
native_american = race["native american"]
all_race = [asian, biracial, hispanic, white, black, native_american]
plt.hist (all_race)
plt.show ()
当前直方图的图像:
我想更改它,使其包含所有种族的名称。 x 和 y 轴上的数字是错误的,我不确定如何修复它
部分数据:
最佳答案
我认为您想要使用的是条形图。直方图用于检查数值数据的分布。以下是我将如何实现它:
import matplotlib.pyplot as plt
import education
list_of_record = education.get_all_states
virginia_education = education.get_state('Virginia')
enrollment = virginia_education["enrollment"]
students = enrollment["students"]
race = students["race"]
x = range(len(race)) # x-axis list
label = [] # x-labels list
y = [] # y-values list
for race, value in race.items():
label.append(race) # add race to x-labels list
y.append(value) # add value to y-values list
plt.bar(x,y,color='indianred',tick_label=label,align='center')
plt.show()
关于python - 使用 matplotlib 绘制直方图或散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36945546/