使用matplotlib绘制直方图或散点图

4
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轴上的数字是错误的,我不确定如何修复。

一些数据:

输入图像说明


如果您能在问题的代码中包含(小)一部分实际数据,那将是非常好的。这样,试图帮助您的人可以立即运行您的示例,进行调试,并验证他们的潜在解决方案。 - Lukas Graf
1个回答

2

我认为你想要使用的是条形图。直方图用于检查数值数据的分布。以下是我实现它的方式:

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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接