gpt4 book ai didi

python 3 pandas 和 seaborn 挣扎使用 swarmplot - multiIndex

转载 作者:太空狗 更新时间:2023-10-30 02:42:30 25 4
gpt4 key购买 nike

我正在努力让 swarmplotpandas 一起工作。我有一个名为 SIAggs 的 3d numpy array,我使用 pandas 将其切片,如下所示:

   rand_center = {('Random_dist'):SIAggs[:,:,1], ('Center_distance'):SIAggs[:,:,0]}

for key, value in rand_center.items():
rand_center[key] = pd.DataFrame(value)

sizes = {}
for i in range(size_iterations):
sizes.update({ (str(i+1)) : SIAggs[i,:,:] })
for key, value in sizes.items():
sizes[key] = pd.DataFrame(value)

df = pd.concat(sizes, rand_center, names = ['sizes', 'distance_measure'])
df.stack()

当我打印 DataFrame 时,它给我:

sizes                  1              2              3       
distance_measure 0 1 0 1 0 1
0 -2.1881 1.262 -2.7001 1.493 -2.1381 1.626
1 -2.3671 1.699 -2.4431 1.208 -2.4571 1.186
2 -2.3071 0.716 -2.2841 1.122 -2.2441 1.396
3 -2.2521 0.967 -1.9451 1.496 -2.5261 1.690
4 -2.4651 1.800 -2.3421 1.500 -2.3571 0.985
5 -2.2011 1.409 -1.9921 0.160 -2.3701 1.114
6 -2.6911 0.915 -3.3301 1.510 -2.2561 1.676
7 -2.5751 1.128 -1.9931 0.941 -2.4411 1.605
8 -2.5321 1.651 -2.4751 1.145 -3.3541 1.228
9 -1.9741 0.886 -2.6671 1.196 -2.4581 1.321

这似乎是对的。

然后,当我尝试用 swarmplot 绘制它时,我想要一个 Series(按我的 sizes 类别)配对(色调差异使用 distance_measure) - 基本上只使用 seaborn website 中的第 5 个示例

ax = sns.swarmplot(x = "sizes", hue = "distance_measure", data = df, split=True)
plt.show()

出现错误:

    ax = sns.swarmplot(x = "sizes", hue = "distance_measure", data = df, split=True)
File "/Users/scottjg/anaconda/lib/python3.5/site-packages/seaborn/categorical.py", line 2679, in swarmplot
split, orient, color, palette)
File "/Users/scottjg/anaconda/lib/python3.5/site-packages/seaborn/categorical.py", line 1179, in __init__
self.establish_variables(x, y, hue, data, orient, order, hue_order)
File "/Users/scottjg/anaconda/lib/python3.5/site-packages/seaborn/categorical.py", line 147, in establish_variables
raise ValueError(err)
ValueError: Could not interpret input 'sizes'

如有任何帮助,我们将不胜感激。我似乎无法与 pandas/seaborn 交 friend ,但我想交 friend !

最佳答案

这里尝试生成您似乎正在寻找的图表,从您的示例数据开始:

df = pd.read_csv('swarm.csv', header=[0, 1], tupleize_cols=True, index_col=None)
cols = ['sizes', 'distance_measure']
df.columns = pd.MultiIndex.from_tuples(df.columns, names=cols)

sizes 1 2
distance_measure 0 1 0
0 -2.1881 1.262 -2.7001
1 -2.3671 1.699 -2.4431
2 -2.3071 0.716 -2.2841
3 -2.2521 0.967 -1.9451
4 -2.4651 1.800 -2.3421

用于 seaborns 演示的样本数据在不同的列中有变量,而不是使用 MultiIndex,所以我相应地进行转换:

df = df.stack(cols).reset_index(cols).rename(columns={0: 'value'})
df.info()

Int64Index: 30 entries, 0 to 9
Data columns (total 3 columns):
sizes 30 non-null object
distance_measure 30 non-null object
value 30 non-null float64

df.head()

sizes distance_measure value
0 1 0 -2.1881
0 1 1 1.2620
0 2 0 -2.7001
1 1 0 -2.3671
1 1 1 1.6990

然后,下面的代码生成类似于示例 #5 的内容:

ax = sns.swarmplot(x="sizes", y='value', hue="distance_measure", data=df, split=True)
plt.show()

enter image description here

关于python 3 pandas 和 seaborn 挣扎使用 swarmplot - multiIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36359172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com