Issue
I'm trying to do a simple barchart on some data.
I've got a list of values x:
objective_values = np.array([ 0., -1., -1., 0., -1., 0., 0., 3., -1., -2., -2., -1., 0., 1. , 1., 4.])
and associated probabilities:
probabilites = np.array([0.05664819, 0.09759888, 0.09759888 ,0.02133367, 0.14491666, 0.03902614, 0.03902614, 0.00385143, 0.08802889, 0.15658646, 0.15658646, 0.03704651, 0.04752267, 0.00210716, 0.00210716, 0.01001469])
which I'm trying to whack into a barchart together. However, when I do the simple code
objective,probabilities,objective_values = compute_mwis_energy_sv_bar_chart(statevector,G)
plt.bar(objective_values,probabilities)
Matplot doesn't sum the ordered probabilities I.e the value of -2 is 0.156, not 0.30. I've scoured all over but I can't find a way to do this simply - am I missing something ?
Solution
You can use .zip() to create a dictionary where you yourself sum up the probabilities for each objective value. Then pass that on to matplotlib
sums = {}
for key, value in zip(objective_values, probabilites):
try:
sums[key] += value
except KeyError:
sums[key] = value
print(sums)
Sums would be
{0.0: 0.20355680999999998, -1.0: 0.46518982000000003, 3.0: 0.00385143, -2.0: 0.31317292, 1.0: 0.00421432, 4.0: 0.01001469}
Then get the keys and values to get the lists
objective_values = list(sums.keys())
probabilites = list(sums.values())
This will give you
([0.0, -1.0, 3.0, -2.0, 1.0, 4.0],
[0.20355680999999998,
0.46518982000000003,
0.00385143,
0.31317292,
0.00421432,
0.01001469])
Answered By - Shubham Periwal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.