简体   繁体   中英

Bar chart creation error "TypeError: unhashable type: 'numpy.ndarray' "

I want to create a bar chart that indicates the frequency of routes between several locations.

The y label represents the number of times a certain connection is made. The x label represents the connections

LBB is a list that contains the list of connections between locations: This is its format: [('A', '->', 'B'), ('B', '->', 'D'), ('F', '->', 'B'), ('A', '->', 'C'), ('C', '->', 'A')]

BB is a list that contains the frequency that each of the connections in LBB happens: This is its format: [1, 9, 1, 10, 64]

Notice that every component of LBB is connected to the respective BB in the same position.

This is the code that i am using to create the chart

   import matplotlib.pyplot as plt
   plt.bar(LBB,BB)
   plt.show()

The error that i get everytime is the following: TypeError: unhashable type: 'numpy.ndarray'

I'm making this question because every other question i've seen online only focuses on dates on the x axis, and never on just normal text.

So if anyone could please help me i would be very thankfull!

You cannot use tuples as categorical x-axis data but we can convert them into strings which can be easily plotted as categorical data :

import matplotlib.pyplot as plt

LBB = [('A', '->', 'B'), ('B', '->', 'D'), ('F', '->', 'B'), ('A', '->', 'C'), ('C', '->', 'A')]
BB = [1, 9, 1, 10, 64]

plt.bar(["".join(b) for b in LBB], BB)
plt.show()

Sample output 在此处输入图像描述 :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM