matplotlib TypeError:不可哈希的类型:'numpy.ndarray'将数据集用于值

db2dz4w8  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(188)

我想做一个条形图,我可以看到一个国家的3枚奖牌。我已经放弃了所有的球队,我只有一个,但当我这样做,我得到这个错误...这是我得到的:

N = 3
ind = np.arange(N) 
width = 0.25

goldMedals = df[(df.Medal == 'Gold')]
bar1 = plt.bar(ind, goldMedals, width, color = 'gold')
  
silverMedals = df[(df.Medal == 'Silver')]
bar2 = plt.bar(ind+width, silverMedals, width, color='bronze')
  
bronzeMedals = df[(df.Medal == 'Bronze')]
bar3 = plt.bar(ind+width*2, bronzeMedals, width, color = 'b')
  
plt.xlabel("Medal")
plt.ylabel('Count')
plt.title("Medal Portugal")
  
plt.xticks(ind+width,['Gold', 'Bronze', 'Silver'])
plt.legend( (bar1, bar2, bar3), ('Gold', 'Bronze', 'Silver') )
plt.show()
rxztt3cl

rxztt3cl1#

我是猜测,因为您没有提供任何数据,但我认为您试图在一个长格式数据框中可视化条件提取的结果。错误的原因是提取结果是一个数据框,但索引和数据框本身是分别指定的。因此,对于每个图形,数据框的索引和y轴值是指定的。

import pandas as pd
import numpy as np
import io

data = '''
 nation  medal  count
0 "South Korea" gold 24
1 China  gold  10
2 Canada  gold  9
3 "South Korea"  silver  13
4  China  silver  15
5  Canada  silver  12
6  "South Korea"  bronze  11
7  China  bronze  8
8  Canada  bronze  12
'''

df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
df = df.query('nation == "Canada"')

import matplotlib.pyplot as plt

N = 3
ind = np.arange(N) 
width = 0.25

goldMedals = df[df.medal == 'gold']
bar1 = plt.bar(goldMedals.medal, goldMedals['count'], width, color='gold')

silverMedals = df[(df.medal == 'silver')]
bar2 = plt.bar(silverMedals.medal, silverMedals['count'], width, color='gray')
  
bronzeMedals = df[(df.medal == 'bronze')]
bar3 = plt.bar(bronzeMedals.medal, bronzeMedals['count'], width, color='brown')
  
plt.xlabel("Medal")
plt.ylabel('Count')
plt.title("Medal Canada")
  
#plt.xticks(ind+width,['Gold', 'Silver','Bronze'])
plt.legend((bar1, bar2, bar3), ('Gold', 'Bronze', 'Silver'))
plt.show()

相关问题