1
votes

I am trying to create a three column bar chart from some of the Counter dictionaries I generated but can't get it to work.

I was able to generate a bar chart of one of the Counter dictionaries but not all three with a column dictionary at each index.

f_cond_count=dict(Counter(f_cond_plot))
f_pow_count=dict(Counter(f_pow_plot))
f_mom_count=dict(Counter(f_mom_plot))


print("cond",f_cond_count)
print("pow",f_pow_count)
print("mom",f_mom_count)

df = pd.DataFrame.from_dict((f_cond_count, f_pow_count, f_mom_count), 'index')
df.plot.bar()
plt.show()

This line of code will print the following:

cond {0.1: 33, 0.2: 36, 0.30000000000000004: 36, 0.4: 36, 0.5: 39, 0.6: 39, 0.7000000000000001: 39, 0.8: 39, 0.9: 39}

pow {0.7000000000000001: 54, 0.8: 81, 0.9: 201}

mom {0.4: 94, 0.5: 27, 0.6: 27, 0.7000000000000001: 18, 0.8: 9, 0.9: 9, 0.30000000000000004: 85, 0.2: 67}

And I can get one dictionary to work if I use the following:

df = pd.DataFrame.from_dict(f_cond_count, 'index')
df.plot.bar()
plt.show()

Where I get a bar chart with [0.1, 0.2, 0.30000000004, 0.4 etc.] on the x-axis and the numbers [33, 36, 36, 36 etc] on the y-axis.

I get the error:"'tuple' object has no attribute 'values'" when I try to do all three and Im not sure why. I'm also not sure if it will work since the dictionaries are different sizes and some don't have entries for certain indices. I am trying to create something similar to the solution posted in this thread matplotlib: plot multiple columns of pandas data frame on the bar chart

1

1 Answers

0
votes

You can first create a DataFrame out of the three dictionaries using Series and then plot all three bar charts at once. As you notice, the values on the x-ticklabels have floating point issues.

The last two lines are added because otherwise the tick-labels show precision problems

df = pd.DataFrame({'cond': pd.Series(f_cond_count), 'powc': pd.Series(f_pow_count), 
                   'mom': pd.Series(f_mom_count) })

ax = df.plot.bar()
labs = [round(float(t.get_text()), 1) for t in ax.axes.get_xmajorticklabels()]
ax.set_xticklabels(labs);

enter image description here