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

≡放荡痞女 提交于 2019-12-03 15:56:12

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

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 seaborn to do the color-mapping the way you expect (as discussed here), although it doesn't do marker style mapping:

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!