[英]How to plot a swarmplot of a grid of data, on one plot with seaborn
I having been trying for ages to plot this data the way I want on seaborn, from a pandas dataframe. Any advice would be appreciated.我已经尝试了 plot seaborn 上的数据,来自 pandas dataframe。任何建议将不胜感激。
The Data is in a dataframe and looks like this, with 12 months, and 11 columns plus a month column:数据在 dataframe 中,看起来像这样,有 12 个月,11 列加上一个月列:
Month FSRPX1M VOX1M XLB1M ... XLP1M XLU1M XLV1M XLY1M
0 Jan 0.087643 -0.561428 -0.409286 ... -0.177143 0.232858 0.521428 0.457857
1 Feb 0.132429 -0.265715 0.470715 ... 0.067142 -0.479286 -0.177143 0.363571
2 Mar 0.152429 1.002142 0.437857 ... 0.588573 0.671428 0.055000 0.727856
3 Apr 0.150071 2.445000 1.331428 ... 0.551428 1.100715 0.790715 2.175714
4 May -0.089429 -0.115714 0.275000 ... 0.240000 0.113572 0.650716 -0.366429
What I want to do is plot a swarm plot, with Months along the X Axis, and the number along the Y axis, with 11 'dots' for each month, representing the 11 columns, with an associated a color coded key.我想要做的是 plot 群 plot,X 轴为月份,Y 轴为数字,每个月有 11 个“点”,代表 11 列,并带有相关的颜色编码键。
I have tried various things including [where df is the dataframe and val_list is a list of the columns I want as dots]:我尝试了各种方法,包括 [其中 df 是 dataframe,val_list 是我想要作为点的列的列表]:
sns.swarmplot(data = df, x=df['Month'], y = df[val_list])
and和
sns.swarmplot(data = df, x=df['Month'], hue = df[val_list])
as well as trying to slice the dataframe, and make a plot, one column at a time using a for loop:以及尝试对 dataframe 进行切片,并生成 plot,使用 for 循环一次一列:
for e in val_list:
sns.stripplot(data = df, x=df['Month'], y = df[e])
Any help and explanation of what I'm doing wrong would be great, thanks对我做错了什么的任何帮助和解释都会很棒,谢谢
The last one makes a plot, but all the dots are the same color and unlabelled最后一个是 plot,但是所有的点都是相同的颜色并且没有标记
To work with hue
, Seaborn needs the data in "long form" .要使用hue
, Seaborn 需要“长格式”的数据。 Pandas' melt
can be used to convert a "wide" dataframe to a "long" one. Pandas 的melt
可用于将“宽”dataframe 转换为“长”。 The Set3
colormap has 12 colors (the default tab10
only has 10). Set3
颜色图有 12 个 colors(默认的tab10
只有 10 个)。
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
elements = ['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron', 'Carbon',
'Nitrogen', 'Oxygen', 'Fluorine', 'Neon', 'Sodium']
df = pd.DataFrame(np.random.randn(12, len(elements)), columns=elements)
df['Month'] = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
df_long = df.melt(id_vars='Month', var_name='Element', value_name='Value')
ax = sns.swarmplot(data=df_long, x='Month', y='Value', hue='Element', palette='Set3')
ax.legend(bbox_to_anchor=(1.01, 1.02), loc='upper left', title='Element')
plt.tight_layout()
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.