pandas 有没有办法根据不同列中的离散变量制作 matplotlib 散点图标记或颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24297097/
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-09-13 22:10:29  来源:igfitidea点击:

Is there a way to make matplotlib scatter plot marker or color according to a discrete variable in a different column?

pythonmatplotlibpandasscatter

提问by Charlie_M

I'm making scatterplots out of a DF using matplotlib. In order to get different colors for each data set, I'm making two separate calls to plt.scatter:

我正在使用 matplotlib 从 DF 中制作散点图。为了为每个数据集获得不同的颜色,我对 plt.scatter 进行了两次单独的调用:

plt.scatter(zzz['HFmV'], zzz['LFmV'], label = dut_groups[0], color = 'r' )
plt.scatter(qqq['HFmV'], qqq['LFmV'], label = dut_groups[1], color = 'b' )
plt.legend()
plt.show()

This gives me the desired color dependence but really what would be ideal is if I could just get pandas to give me the scatterplot with several datasets on the same plot by something like

这给了我所需的颜色依赖性,但真正理想的是,如果我可以让Pandas通过类似的方式在同一图上为我提供带有多个数据集的散点图

df.plot(kind = scatter(x,y, color = df.Group, marker = df.Head)

df.plot(kind = scatter(x,y, color = df.Group, 标记 = df.Head)

Apparently there is no such animal (at least that I could find). So, next best thing in my mind would be to put the plt.scatter calls into a loop where I could make the color or marker vary according to one of the rows (not x or y, but some other row. If the row I want to use were a continuous variable it looks like I could use a colormap, but in my case the row I need to sue for this is a string ( categorical type of variable, not a number).

显然没有这样的动物(至少我能找到)。因此,我认为下一个最好的事情是将 plt.scatter 调用放入一个循环中,我可以在其中根据行之一(不是 x 或 y,而是其他行)使颜色或标记发生变化。如果行我想要使用的是一个连续变量,看起来我可以使用颜色图,但在我的情况下,我需要为此起诉的行是一个字符串(变量的分类类型,而不是数字)。

Any help much appreciated.

非常感谢任何帮助。

回答by mwaskom

What you're doing will almost work, but you have to pass colora vector of colors, not just a vector of variables. So you could do:

你正在做的几乎可以工作,但你必须传递color一个颜色向量,而不仅仅是一个变量向量。所以你可以这样做:

color = df.Group.map({dut_groups[0]: "r", dut_groups[1]: "b"})
plt.scatter(x, y, color=color)

Same goes for the marker style

标记样式也是如此

You could also use seabornto do the color-mapping the way you expect (as discussed here), although it doesn't do marker style mapping:

你也可以使用seaborn做色彩映射你希望(如讨论的方式在这里),虽然它没有做标记样式映射:

import seaborn as sns
import pandas as pd
from numpy.random import randn

data = pd.DataFrame(dict(x=randn(40), y=randn(40), g=["a", "b"] * 20))
sns.lmplot("x", "y", hue="g", data=data, fit_reg=False)

enter image description here

enter image description here