在 seaborn.jointplot 中绘制两个分布
Plotting two distributions in seaborn.jointplot
我有两个 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)
两个数据帧中的键是相同的。
如何在同一图中绘制我的值(当然颜色不同)?甚至更详细:如何绘制两个数据集,但只在顶部和侧面分布第一个数据集? IE。只绘制点。
这是一种通过修改 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)
绘制联合图后可能会更容易,更改为要绘制内容的轴,然后使用普通的 pyplot 或基于轴的 seaborn 图:
g=sns.jointplot(...)
plt.sca("axis_name")
plt.plot/plt.scatter/.../sns.kde(ax="axis_name")
二维图的轴名称是ax_joint
,侧面的一维图是ax_marg_x
或ax_marg_y
。
此外,如果您想使用 jointplot 结构但通过 pyplot 绘制所有图,请使用 cla
函数,例如用于清除二维图:
g.ax_joint.cla()
在我看来,更好的解决方案是对 sns.joinplot
returns 的联合分布和边缘分布使用坐标轴句柄。使用这些(名称为 ax_joint
、ax_marg_x
和 ax_marg_y
)也可以绘制边际分布图。
import seaborn as sns
import numpy as np
data1 = np.random.randn(100)
data2 = np.random.randn(100)
data3 = np.random.randn(100)
data4 = np.random.randn(100)
df1 = pd.DataFrame({'col1': data1, 'col2':data2})
df2 = pd.DataFrame({'col1': data3, 'col2':data4})
axs = sns.jointplot('col1', 'col2', data=df1)
axs.ax_joint.scatter('col1', 'col2', data=df2, c='r', marker='x')
# drawing pdf instead of histograms on the marginal axes
axs.ax_marg_x.cla()
axs.ax_marg_y.cla()
sns.distplot(df1.col1, ax=axs.ax_marg_x)
sns.distplot(df1.col2, ax=axs.ax_marg_y, vertical=True)
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")
我有两个 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)
两个数据帧中的键是相同的。
如何在同一图中绘制我的值(当然颜色不同)?甚至更详细:如何绘制两个数据集,但只在顶部和侧面分布第一个数据集? IE。只绘制点。
这是一种通过修改 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)
绘制联合图后可能会更容易,更改为要绘制内容的轴,然后使用普通的 pyplot 或基于轴的 seaborn 图:
g=sns.jointplot(...)
plt.sca("axis_name")
plt.plot/plt.scatter/.../sns.kde(ax="axis_name")
二维图的轴名称是ax_joint
,侧面的一维图是ax_marg_x
或ax_marg_y
。
此外,如果您想使用 jointplot 结构但通过 pyplot 绘制所有图,请使用 cla
函数,例如用于清除二维图:
g.ax_joint.cla()
在我看来,更好的解决方案是对 sns.joinplot
returns 的联合分布和边缘分布使用坐标轴句柄。使用这些(名称为 ax_joint
、ax_marg_x
和 ax_marg_y
)也可以绘制边际分布图。
import seaborn as sns
import numpy as np
data1 = np.random.randn(100)
data2 = np.random.randn(100)
data3 = np.random.randn(100)
data4 = np.random.randn(100)
df1 = pd.DataFrame({'col1': data1, 'col2':data2})
df2 = pd.DataFrame({'col1': data3, 'col2':data4})
axs = sns.jointplot('col1', 'col2', data=df1)
axs.ax_joint.scatter('col1', 'col2', data=df2, c='r', marker='x')
# drawing pdf instead of histograms on the marginal axes
axs.ax_marg_x.cla()
axs.ax_marg_y.cla()
sns.distplot(df1.col1, ax=axs.ax_marg_x)
sns.distplot(df1.col2, ax=axs.ax_marg_y, vertical=True)
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")