'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!
Solution 1:[1]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

