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

时间:2014-11-18 23:00:45

标签: python matplotlib seaborn

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

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并手动绘制数据和边际分布。

6 个答案:

答案 0 :(得分:18)

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

这给了我这个:

enter image description here

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

答案 1 :(得分:14)

接受的答案太复杂了。 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)))

答案 2 :(得分:3)

由于关键字: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})

答案 3 :(得分:1)

  1. seaborn/categorical.py中,找到def swarmplot
  2. marker='o'
  3. 之前添加参数**kwargs
  4. kwargs.update中,添加marker=marker
  5. 然后添加例如使用marker='x'绘制时,sns.swarmplot()作为参数,就像使用Matplotlib plt.scatter()一样。

    刚刚遇到同样的需求,marker作为kwarg无效。所以我简短地看了一下。我们可以用类似的方式设置其他参数。 https://github.com/ccneko/seaborn/blob/master/seaborn/categorical.py

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

答案 4 :(得分:0)

另一种选择是使用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")

答案 5 :(得分:-1)

另外两个答案是复杂的奢侈品(实际上,他们是真正了解幕后发生的事情的人)。

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

let desktopDict = NSUserDefaults.standardUserDefaults().persistentDomainForName("com.apple.desktop")
相关问题