python - Seaborn columnwise violinplot
问题描述
I have a dict d
which lists numbers' occurences:
{'item1': [42, 1, 2, 3, 42, 2, 1, 1, 1, 1, 1],
'item2': [2, 5],
'item3': [5, 1, 7, 2, 7, 1, 42, 2, 9]}
Which I then convert to a DataFrame counting these occurences:
df = pd.DataFrame.from_dict({k: dict(Counter(v)) for k, v in d.items()})
item1 item2 item3
42 2.0 NaN 1.0
1 6.0 NaN 2.0
2 2.0 1.0 2.0
3 1.0 NaN NaN
5 NaN 1.0 1.0
7 NaN NaN 2.0
9 NaN NaN 1.0
How can I plot this or some other DataFrame that was derived from d
using seaborn.violinplot
, so that each column in the dataframe represents a violin in the plot based on the data provided by each columns values and their respective indices?
I have tried multiple combinations of which I believe this intuitively comes the closest, but unfortunately still fails:
sns.violinplot(x=df.keys(), y=df.index, data=df)
解决方案
Pass DataFrame
to seaborn.violinplot
, so each Series (column) is plotted separately:
sns.violinplot(data=df)
推荐阅读
- reactjs - React 在现有状态转换期间无法更新
- cmd - 为什么在使用 assoc 命令将特定扩展名更改为另一个不同时在 windows 中打开程序没有任何改变?
- php - 为onclick javascript函数传递参数时如何在php中添加单引号?
- c - 如何使用 strdup 分配具有给定 char 数组的结构?
- python - HIVE 没有与 Python 连接?
- python - 如何在使用 selenium 时输入文件名并按 Enter 上传文件
- git - 无法从 Docker 上运行的 Git 存储库中提取
- html - 本地连接css字体
- c++ - 如何修复链表类实现的未处理异常 0xDDDDDDDD?
- java - 每当 Android 谷歌地图的位置发生变化时相机如何移动