图例按组着色的散点图,无需多次调用plt.scatter [英] scatter plot with legend colored by group without multiple calls to plt.scatter

查看:270
本文介绍了图例按组着色的散点图,无需多次调用plt.scatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pyplot.scatter允许将与组相对应的数组传递给c=,该数组随后将基于这些组为点着色.但是,这似乎不支持在不单独绘制每个组的情况下生成图例.

pyplot.scatter allows for passing to c= an array that corresponds to groups, which will then color the points based on those groups. However, this seems to not support generating a legend without specifically plotting each group separately.

例如,可以通过遍历各组并分别绘制每个图来生成带有彩色组的散点图:

So, for example, a scatter plot with groups colored can be generated by iterating over the groups and plotting each separately:

import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
feats = load_iris()['data']
target = load_iris()['target']

f, ax = plt.subplots(1)
for i in np.unique(target):
    mask = target == i
    plt.scatter(feats[mask, 0], feats[mask, 1], label=i)
ax.legend()

哪个生成:

我可以实现相似的情节而无需遍历每个组:

I can achieve a similar looking plot without iterating over each group though:

f, ax = plt.subplots(1)
ax.scatter(feats[:, 0], feats[:, 1], c=np.array(['C0', 'C1', 'C2'])[target])

但是我无法找到第二种策略来生成相应图例的方法.我遇到的所有示例都在各个组之间进行迭代,这似乎不理想.我知道我可以手动生成图例,但这又显得太麻烦了.

But I cannot figure out a way to generate a corresponding legend with this second strategy. All of the examples I've come across iterate over the groups, which seems...less than ideal. I know I can manually generate a legend, but again that seems overly cumbersome.

推荐答案

解决此问题的matplotlib分散示例也使用了循环,因此可能是预期的用法:

The matplotlib scatter example that addresses this problem also uses a loop, so that is probably the intended usage: https://matplotlib.org/examples/lines_bars_and_markers/scatter_with_legend.html

如果您的主要目标是使分类数据的绘制和标记更加简单明了,则应考虑 Seaborn .这与散点图类似Pandas/Pyplot中的内容:如何按类别进行绘制

If your larger goal is to just make plotting and labeling categorical data more straightforward, you should consider Seaborn. This is a similar question to Scatter plots in Pandas/Pyplot: How to plot by category

实现目标的一种方法是使用带有标记列的熊猫.将数据保存在Pandas数据框中后,您可以使用 Seaborn pairplot 进行制作这种情节. (Seaborn还提供了虹膜数据集作为标记的DataFrame)

A way to accomplish your goal is to use pandas with labeled columns. Once you have data in a Pandas Dataframe, you can use Seaborn pairplot to make this sort of plot. (Seaborn also has the iris dataset available as a labeled DataFrame)

import seaborn as sns
iris = sns.load_dataset("iris")
sns.pairplot(iris, hue="species")

如果仅需要前两个功能,则可以使用

If you just want the first two features, you can use

sns.pairplot(x_vars=['sepal_length'], y_vars=['sepal_width'], data=iris, hue="species", size=5)

如果您真的想使用sklearn数据字典,则可以将其拉入数据框,如下所示:

If you really want to use the sklearn data dict, you can pull that into a dataframe like so:

import pandas as pd
from sklearn.datasets import load_iris
import numpy as np

feats = load_iris()['data'].astype('O')
target = load_iris()['target']
feat_names = load_iris()['feature_names']
target_names = load_iris()['target_names'].astype('O')

sk_df = pd.DataFrame(
    np.hstack([feats,target_names[target][:,np.newaxis]]),
    columns=feat_names+['target',])
sns.pairplot(sk_df, vars=feat_names, hue="target")

这篇关于图例按组着色的散点图,无需多次调用plt.scatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆