在Seaborn FacetGrid中绘制多个DataFrame列 [英] Plot multiple DataFrame columns in Seaborn FacetGrid

查看:117
本文介绍了在Seaborn FacetGrid中绘制多个DataFrame列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码

import seaborn as sns

g = sns.FacetGrid(dataframe, col='A', hue='A')
g.map(plt.plot, 'X', 'Y1')
plt.show()

制作一个像这样的海洋刻面图:

to make a seaborn facet plot like this:

现在,我想在该图上添加另一行,并在y轴上添加一个不同的变量,称为Y2.结果应类似于垂直叠加通过

Now I would like to add another row to this plot with a different variable, call it Y2, on the y axis. The result should look similar to vertically stacking the two plots obtained by

g = sns.FacetGrid(dataframe, col='A', hue='A')
g.map(plt.plot, 'X', 'Y1')
plt.show()

g = sns.FacetGrid(dataframe, col='A', hue='A')
g.map(plt.plot, 'X', 'Y2')
plt.show()

,但在一个图中,没有重复的x轴和标题("A =< value>"),也没有创建新的FacetGrid对象.

but in a single plot, without the duplicate x axis and titles ("A=<value>") and without creating a new FacetGrid object.

请注意

g = sns.FacetGrid(dataframe, col='A', hue='A')
g.map(plt.plot, 'X', 'Y1')
g.map(plt.plot, 'X', 'Y2')
plt.show()

之所以无法实现,是因为对于每个A值,Y1和Y2的曲线都显示在同一子图中.

does not achive this, because it results in both the curve for Y1 and Y2 being displayed in the same subplot for each value of A.

推荐答案

我使用以下代码创建了一个看起来与您匹配的综合数据集:

I used the following code to create a synthetic dataset which appears to match yours:

import pandas
import numpy
import seaborn as sns
import matplotlib.pyplot as plt

# Generate synthetic data
omega = numpy.linspace(0, 50)

A0s = [1., 18., 40., 100.]

dfs = []
for A0 in A0s:
    V_w_dr = numpy.sin(A0*omega)
    V_w_tr = numpy.cos(A0*omega)
    dfs.append(pandas.DataFrame({'omega': omega,
                                 'V_w_dr': V_w_dr,
                                 'V_w_tr': V_w_tr,
                                 'A0': A0}))
dataframe = pandas.concat(dfs, axis=0)

然后,您可以做您想做的事(感谢)sharey='row', margin_titles=True的注释中的@mwaskom):

Then you can do what you want (thanks to @mwaskom in the comments for )sharey='row', margin_titles=True):

melted = dataframe.melt(id_vars=['A0', 'omega'], value_vars=['V_w_dr', 'V_w_tr'])
g = sns.FacetGrid(melted, col='A0', hue='A0', row='variable', sharey='row', margin_titles=True)
g.map(plt.plot, 'omega', 'value')

这导致

这篇关于在Seaborn FacetGrid中绘制多个DataFrame列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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