使用seaborn jointplot改变每个点的颜色和标记

pbr*_*ach 14 python matplotlib seaborn

我从这里稍微修改了这段代码:

import seaborn as sns
sns.set(style="darkgrid")

tips = sns.load_dataset("tips")
color = sns.color_palette()[5]
g = sns.jointplot("total_bill", "tip", data=tips, kind="reg", stat_func=None,
                  xlim=(0, 60), ylim=(0, 12), color='k', size=7)

g.set_axis_labels('total bill', 'tip', fontsize=16)
Run Code Online (Sandbox Code Playgroud)

我得到了一个漂亮的情节 - 但是,对于我的情况,我需要能够改变每个点的颜色和格式.

我使用的关键字尝试,marker,style,和fmt,但我得到的错误TypeError: jointplot() got an unexpected keyword argument.

这样做的正确方法是什么?我想避免sns.JointGrid手动调用和绘制数据和边际分布.

pbr*_*ach 19

解决这个问题与matplotlib(使用不同的标记和颜色绘制散点图)几乎没有什么不同,除了我想保留边缘分布:

import seaborn as sns
from itertools import product
sns.set(style="darkgrid")

tips = sns.load_dataset("tips")
color = sns.color_palette()[5]
g = sns.jointplot("total_bill", "tip", data=tips, kind="reg", stat_func=None,
                  xlim=(0, 60), ylim=(0, 12), color='k', size=7)

#Clear the axes containing the scatter plot
g.ax_joint.cla()

#Generate some colors and markers
colors = np.random.random((len(tips),3))
markers = ['x','o','v','^','<']*100

#Plot each individual point separately
for i,row in enumerate(tips.values):
    g.ax_joint.plot(row[0], row[1], color=colors[i], marker=markers[i])

g.set_axis_labels('total bill', 'tip', fontsize=16)
Run Code Online (Sandbox Code Playgroud)

这给了我这个:

在此输入图像描述

回归线现在消失了,但这就是我所需要的.

  • `plt.scatter`没有任何方法来控制用于各个点的标记,所以像这样的东西可能是你最好的选择,但你可以做`g.ax_joint.collections [0] .set_visible(False)`清除整个轴,这将保留回归线. (7认同)

Max*_*ron 15

接受的答案太复杂了.plt.sca()可以用更简单的方式来做到这一点:

import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")
g = sns.jointplot("total_bill", "tip", data=tips, kind="reg", stat_func=None,
                  xlim=(0, 60), ylim=(0, 12))


g.ax_joint.cla() # or g.ax_joint.collections[0].set_visible(False), as per mwaskom's comment

# set the current axis to be the joint plot's axis
plt.sca(g.ax_joint)

# plt.scatter takes a 'c' keyword for color
# you can also pass an array of floats and use the 'cmap' keyword to
# convert them into a colormap
plt.scatter(tips.total_bill, tips.tip, c=np.random.random((len(tips), 3)))
Run Code Online (Sandbox Code Playgroud)

  • 我建议跳过调用`plt.sca`并直接使用axes对象:`g.ax_joint.scatter(tips.total_bill,...)`.尽可能避免使用`pyplot`状态机. (4认同)