gpt4 book ai didi

python - 在 seaborn.jointplot 中绘制两个分布

转载 作者:太空狗 更新时间:2023-10-29 21:15:02 24 4
gpt4 key购买 nike

我有两个 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)

enter image description here

关于python - 在 seaborn.jointplot 中绘制两个分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31539815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com