带有多个值的Python Matplotlib图字典 [英] Python matplotlib plot dict with multiple values

查看:174
本文介绍了带有多个值的Python Matplotlib图字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib绘制字典,像这样(只是更多数据):

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

我想制作字典中每个键的一个箱形图/小提琴图(比添加均值,标准偏差等),例如:



但帖子类似:


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:

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.

解决方案

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()

这篇关于带有多个值的Python Matplotlib图字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆