19

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

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)

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

我尝试使用关键字 、markerstylefmt但我得到了错误TypeError: jointplot() got an unexpected keyword argument

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

4

6 回答 6

26

解决这个问题与 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)

这给了我这个:

在此处输入图像描述

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

于 2014-11-19T02:17:23.143 回答
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)))
于 2015-10-14T17:21:53.697 回答
4

由于关键字 : joint_kws(使用 seaborn 0.8.1 测试),您还可以直接在参数列表中对其进行精确化。如果需要,您还可以更改边缘的属性marginal_kws

所以你的代码变成:

import seaborn as sns
colors = np.random.random((len(tips),3))
markers = (['x','o','v','^','<']*100)[:len(tips)]

sns.jointplot("total_bill", "tip", data=tips, kind="reg",
    joint_kws={"color":colors, "marker":markers})
于 2018-03-22T16:09:52.480 回答
2
  1. seaborn/categorical.py中,找到def swarmplot
  2. marker='o'在前面添加参数**kwargs
  3. kwargs.update中,添加marker=marker

然后在绘图时添加 egmarker='x'作为参数,sns.swarmplot()就像使用 Matplotlib 一样plt.scatter()

只是遇到了同样的需求,markerkwarg没有奏效。所以我看了一眼。我们可以用类似的方法设置其他参数。 https://github.com/ccneko/seaborn/blob/master/seaborn/categorical.py

这里只需要做一点小改动,但这里是 GitHub 分叉页面以供快速参考;)

于 2017-11-26T10:58:10.343 回答
1

另一种选择是使用 JointGrid,因为jointplot 是一个简化其使用的包装器。

import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")

g = sns.JointGrid("total_bill", "tip", data=tips)
g = g.plot_joint(plt.scatter, c=np.random.random((len(tips), 3)))
g = g.plot_marginals(sns.distplot, kde=True, color="k")
于 2017-09-08T15:03:08.160 回答
-1

其他两个答案是复杂的奢侈(实际上,它们是由真正了解幕后情况的人提出的)。

这是一个只是猜测的人的答案。它虽然有效!

tips = sns.load_dataset("tips")
g = sns.jointplot("total_bill", "tip", data=tips,
              c=tips.day.cat.codes, cmap='Set1', stat_func=None,
              xlim=(0, 60), ylim=(0, 12))
于 2016-04-19T02:03:05.827 回答