Plotting two distributions in seaborn.jointplot

≡放荡痞女 提交于 2019-12-18 03:35:17

问题


I have two pandas dataframes I would like to plot in the same seaborn jointplot. It looks something like this (commands are don in an 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.


回答1:


Here is one way to do it by modifying the underlying data of 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)




回答2:


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:

g=sns.jointplot(...)
plt.sca("axis_name")
plt.plot/plt.scatter/.../sns.kde(ax="axis_name")

The axis name is either ax_joint for the 2d-Plot or ax_marg_x or ax_marg_y for the 1d Plots on the side.

Furthermore, if you want to use the jointplot structure but plot all plots by pyplot, use the cla function, e.g. for clearing the 2d-Plot:

g.ax_joint.cla()


来源:https://stackoverflow.com/questions/31539815/plotting-two-distributions-in-seaborn-jointplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!