pandas 在 seaborn.jointplot 中绘制两个分布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31539815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plotting two distributions in seaborn.jointplot
提问by Daniel Thaagaard Andreasen
I have two pandasdataframes I would like to plot in the same seaborn jointplot. It looks something like this (commands are don in an IPython shell; ipython --pylab):
我有两个pandas数据框我想在同一个 seaborn jointplot 中绘制。它看起来像这样(命令在 IPython shell 中;ipython --pylab):
import pandas as pd
import seaborn as sns
iris = sns.load_dataset('iris')
df = pd.read_csv('my_dataset.csv')
g = sns.jointplot('sepal_length', 'sepal_width', iris)
The keys in the two dataframes are identical.
How do I plot my values in the same plot (different color of course)? And even more detailed: How do I plot both dataset, but only having the distribution of the first on at the top and side? I.e. only plot the dots.
两个数据帧中的键是相同的。
如何在同一个图中绘制我的值(当然是不同的颜色)?甚至更详细:如何绘制两个数据集,但仅在顶部和侧面具有第一个分布?即只绘制点。
回答by Jianxun Li
Here is one way to do it by modifying the underlying data of sns.JointGrid.
这是通过修改sns.JointGrid.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# simulate some artificial data
# ========================================
np.random.seed(0)
data1 = np.random.multivariate_normal([0,0], [[1,0.5],[0.5,1]], size=200)
data2 = np.random.multivariate_normal([0,0], [[1,-0.8],[-0.8,1]], size=100)
# both df1 and df2 have bivaraite normals, df1.size=200, df2.size=100
df1 = pd.DataFrame(data1, columns=['x1', 'y1'])
df2 = pd.DataFrame(data2, columns=['x2', 'y2'])
# plot
# ========================================
graph = sns.jointplot(x=df1.x1, y=df1.y1, color='r')
graph.x = df2.x2
graph.y = df2.y2
graph.plot_joint(plt.scatter, marker='x', c='b', s=50)


回答by Guiste
It might be easier after drawing the jointplot, change to the axis on which you want to draw something and use then normal pyplot or axis based seaborn plots:
绘制关节图后可能会更容易,更改为要在其上绘制内容的轴,然后使用普通的 pyplot 或基于轴的 seaborn 图:
g=sns.jointplot(...)
plt.sca("axis_name")
plt.plot/plt.scatter/.../sns.kde(ax="axis_name")
The axis name is either ax_jointfor the 2d-Plot or ax_marg_xor ax_marg_yfor the 1d Plots on the side.
轴名称是ax_joint用于2D-绘图或ax_marg_x或ax_marg_y用于上侧的1D图解。
Furthermore, if you want to use the jointplot structure but plot all plots by pyplot, use the clafunction, e.g. for clearing the 2d-Plot:
此外,如果您想使用联合图结构但通过 pyplot 绘制所有图,请使用该cla函数,例如清除二维图:
g.ax_joint.cla()

