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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27005783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:18:03  来源:igfitidea点击:

Changing color and marker of each point using seaborn jointplot

pythonmatplotlibseaborn

提问by pbreach

I have this code slightly modified from 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)

and I get a nice looking plot - However, for my case I need to be able to change the color AND format of each individual point.

并且我得到了一个漂亮的图 - 但是,就我而言,我需要能够更改每个点的颜色和格式。

I've tried using the keywords, marker, style, and fmt, but I get the error TypeError: jointplot() got an unexpected keyword argument.

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

What is the correct way to do this? I'd like to avoid calling sns.JointGridand plotting the data and marginal distributions manually..

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

采纳答案by pbreach

Solving this problem is almost no different than that from matplotlib (plotting a scatter plot with different markers and colors), except I wanted to keep the marginal distributions:

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

Which gives me this:

这给了我这个:

enter image description here

在此处输入图片说明

The regression line is now gone, but this is all I needed.

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

回答by Max Shron

The accepted answer is too complicated. plt.sca()can be used to do this in a simpler way:

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

回答by riri

The other two answers are complex extravagances (actually, they're by people who truly understand what's going on under the hood).

另外两个答案是复杂的铺张浪费(实际上,它们是由真正了解幕后情况的人提出的)。

Here's an answer by someone who's just guessing. It works though!

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

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))

回答by Vlamir

Another option is to use JointGrid, since jointplot is a wrapper that simplifies its usage.

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

回答by Claire

  1. In seaborn/categorical.py, find def swarmplot.
  2. Add parameter marker='o'before **kwargs
  3. In kwargs.update, add marker=marker.
  1. seaborn/categorical.py,找到def swarmplot
  2. marker='o'在前面添加参数**kwargs
  3. kwargs.update,添加marker=marker

Then add e.g. marker='x'as a parameter when plotting with sns.swarmplot()as you would with Matplotlib plt.scatter().

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

Just came across the same need, and having markeras a kwargdid not work. So I had a brief look. We can set other parameters in similar ways. https://github.com/ccneko/seaborn/blob/master/seaborn/categorical.py

只是遇到了同样的需要,并具有marker作为一个kwarg没有工作。所以我看了一眼。我们可以用类似的方式设置其他参数。 https://github.com/ccneko/seaborn/blob/master/seaborn/categorical.py

Only a small change needed here, but here's the GitHub forked page for quick reference ;)

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

回答by Vincent Jeanselme

You can also directly precise it in the list of arguments, thanks to the keyword : joint_kws(tested with seaborn 0.8.1). If needed, you can also change the properties of the marginal with marginal_kws

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

So your code becomes :

所以你的代码变成:

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})