簡體   English   中英

使用matplotlib繪制直方圖或散點圖

[英]Plotting histogram or scatter plot with matplotlib

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM