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

标签 python matplotlib plot

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

当前直方图的图像:

Image of current histogram

我想更改它,使其包含所有种族的名称。 x 和 y 轴上的数字是错误的,我不确定如何修复它

部分数据:

enter image description here

最佳答案

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

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/

相关文章:

python - 如何在 matplotlib 中绘制具有 2 个以上特征的 sklearn svm 的超平面?

python - 将标准差添加到 pandas groupby 对象的平均值

python - 如何在 Numpy(或其他 Python 解决方案)中利用外积的对称性?

python - 使用 Windows SDK 构建 Python 包的批处理脚本

python - 在迭代图的 for 循环上使用 plt.savefig 返回空白图像

python - 绘制二维数据 : heatmap with different colormaps

r - 使用 ggplot2 绘制 k 均值聚类

python - OpenCV Python 使用 HoughLinesP() 算法测量距离以确定水位

python - matplotlib:仅将标记应用于起点或终点?

r - ggplot2垂直线未插入到所需位置