在seaborn.jointplot中绘制两个分布 [英] Plotting two distributions in seaborn.jointplot

查看:656
本文介绍了在seaborn.jointplot中绘制两个分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个要在同一个seaborn中绘制的pandas数据帧

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)

两个数据框中的键是相同的.
如何在同一图中绘制值(当然是不同的颜色)?甚至更详细:如何绘制两个数据集,但仅在顶部和侧面分布第一个数据集? IE.只画点.

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.

推荐答案

这里是修改sns.JointGrid的基础数据的一种方法.

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)

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

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