I'm doing some clustering which results in a list of strings like this:
['5-3-2', '5-3-2', '4-3-2-1', ...]
I want to plot a bar chart based on the frequency of the strings. Is there an easy way to do this? I think I could identify the unique elements in the list and count them, but maybe there is a more comfortable solution?
EDIT: Further information
import matplotlib.pyplot as plt
import numpy as np
import math as math
import Utils as ut
from sklearn.cluster import KMeans
from itertools import cycle
...
result = np.array(result)
keys, counts = np.unique(result, return_counts=True)
print('Keys: ', keys)
print('Counts: ', counts)
print(result)
plt.bar(keys,counts)
plt.show
Output:
Keys: ['3-1-4-2' '3-2-3-2' '3-3-2-2' '4-2-2-2' '4-2-3-1' '4-4-2']
Counts: [ 21 154 23 1 48 4]
EDIT 2: plot shows in debug mode with breakpoint on plt.show, when I step over it disappears. So its not visible in run mode. Any suggestions?
np.unique can return the counts of the unique elements of a list.
keys, counts = np.unique(x, return_counts=True)
You may then plot those as bar plot.
import matplotlib.pyplot as plt
import numpy as np
x = ['5-3-2', '5-3-2', '4-3-2', "2-3-2", '4-3-2', '4-3-2', "1-2-4"]
keys, counts = np.unique(x, return_counts=True)
plt.bar(keys, counts)
plt.show()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With