简体   繁体   English

Seaborn的多个单一地块与大熊猫groupby数据

[英]Multiple single plots in seaborn with pandas groupby data

My issue is very specific, i guess, but i can't seem to find a proper solution, and im clueless with the error output that i get. 我想我的问题很具体,但是我似乎找不到合适的解决方案,而且我收到的错误输出毫无头绪。 Anyway, i have a pandas dataframe loaded from an sqlite database. 无论如何,我有一个从sqlite数据库加载的pandas数据框。

data_frame = pd.read_sql_query(
"SELECT (total_comb + total_comb_rc) as total_comb, p_val, w_length from {tn}".format(
    tn=table_name), conn)

With that loaded, i group the data by the 'w_length' value. 加载该文件后,我按“ w_length”值对数据进行分组。

for i, group in data_frame.groupby('w_length'):

Now, i want to plot a scatter plot for each group created with seaborn lmplot. 现在,我想为使用seaborn lmplot创建的每个组绘制散点图。

for i, group in data_frame.groupby('w_length'):
    sns.lmplot(x=group['total_comb'], y=group['p_val'],
               data=group,
               fit_reg=False)
    sns.despine()
    plt.savefig('test_scatter'+i+'.png', dpi=400)

But for some reason im getting, this output. 但是由于某种原因,我得到了这个输出。

'[  6.95485628e-02   3.53641178e-01   3.46862200e+06   4.11684800e+06] not in index'

and no plot file. 并且没有情节文件。 I know im doing something wrong, but i cant seem to figure it out. 我知道我做错了事,但我似乎无法弄清楚。

pd: i know i can do something like this. PD:我知道我可以做这样的事情。

sns.lmplot(x='total_comb', y='p_val',
       data=data_frame,
       fit_reg=False,
       hue="w_length", x_jitter=.1, col="w_length", col_wrap=3, size=4)

but i also need the separeted plots for each 'w_length'. 但我还需要每个“ w_length”的分隔图。

Thanks!! 谢谢!!

Supposing the problem is not due to the data collection from the sql database, it's probably due to the fact that you call 假设问题不是由于从sql数据库收集数据,可能是由于您调用了
sns.lmplot(x=group['total_comb'], y=group['p_val'], data=group) instead of sns.lmplot(x=group['total_comb'], y=group['p_val'], data=group)代替
sns.lmplot(x='total_comb', y='p_val', data=group)

Here is a working example, which produces two separate plots: 这是一个工作示例,它产生两个单独的图:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np; np.random.seed(42)

x = np.arange(24)
y = np.random.randint(1,10, len(x))
cat = np.random.choice(["A", "B"], size=len(x))
df = pd.DataFrame({"x": x, "y": y, "cat": cat})

for i, group in df.groupby('cat'):
    sns.lmplot(x="x", y="y", data=group, fit_reg=False)
    plt.savefig(__file__+str(i)+".png")
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM