根据第三个变量matplotlib

时间:2019-12-04 23:32:15

标签: python matplotlib bar-chart

我正在尝试绘制每个国家所占世界囚犯人数的百分比,以百分比最高的10个国家为例。

import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import csv
country=[]; percentage=[];democracy=[] #setting the 3 variables I need to use
with open ('DataSetPrison.csv','r') as csvfile:
    csvreader=csv.DictReader(csvfile)
    for line in csvreader: #read each line from the CSV file and add the values to the corresponding variables set above
        country.append(line['Country'].strip('Â\xa0')) #there was a strange set of characters I needed to remove
        percentage.append(line['Percentage'])
        democracy.append(line['Democracy index'])
percentage= list(map(int,percentage[0:10])) #transform the array of string into integers
democracy= list(map(int,democracy[0:10]))
plt.bar(country[0:10],percentage[0:10]) #ploting the data 
plt.xticks(rotation=80) #rotate the labels on the x axis
plt.grid() #display the grid

Bar chart

但是我试图根据democracy数组[8, 3, 7, 3, 7, 5, 6, 4, 2, 6]为每个栏着色,每个国家/地区都有一个“分数”,例如,最高分数应为绿色,而较低分数应为红色。就像来自分散的colormap

的RdYlGr

因此,例如,美国应该是一个漂亮的绿色,带有8,而中国应该是漂亮的红色,带有3。 在条形图旁边有一个带有显示值的色条

我已经浏览了stackoverflow中存在的一些答案,但是我不明白,尝试几个线索已经2个小时了。 flxbmmbll。

谢谢! PS:欢迎对我的代码发表任何评论,我正在学习

0 个答案:

没有答案
相关问题