1

I am trying to plot a dict with matplotlib, like this (just much more data):

b = {"A": ['26', '44', '10', '22', '26'], "B": ['39', '24'], 'C': ['22', '23'], 'D': ['21', '12']}

I wanted to make one boxplot / violinplot for each key in the dict, (than adding mean, std. deviation, etc.) like: enter image description here

But posts like: Plotting a dictionary with multiple values per key does not work for me, because my keys are letters (coding for amino acids).

I am feeling like i don't see the elephant in the room.

1 Answer 1

3

You need to bring the data in the form of a list of lists and make sure the data is numeric and not strings. You can then plot them using the boxplot or violinplot commands.

import matplotlib.pyplot as plt

b = {"A": ['26', '44', '10', '22', '26'], "B": ['39', '24'], 
     'C': ['22', '23'], 'D': ['21', '12']}

index= []
data = []
for i, (key, val) in enumerate(b.iteritems()):
    index.append(key)
    data.append(map(float, val))

fig, (ax, ax2) = plt.subplots(ncols=2)
ax.boxplot(data)
ax.set_xticklabels(index)
ax2.violinplot(data)
ax2.set_xticks(range(1,len(index)+1))
ax2.set_xticklabels(index) 

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.