[英]scatter plot with legend colored by group without multiple calls to plt.scatter
pyplot.scatter
allows for passing to c=
an array that corresponds to groups, which will then color the points based on those groups. pyplot.scatter
允许传递给c=
对应于组的数组,然后基于这些组对点进行着色。 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()
Which generates: 哪个产生:
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.
我知道我可以手动生成一个图例,但再次看起来过于繁琐。
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 解决此问题的matplotlib分散示例也使用循环,因此可能是预期的用法: https : //matplotlib.org/examples/lines_bars_and_markers/scatter_with_legend.html
If your larger goal is to just make plotting and labeling categorical data more straightforward, you should consider Seaborn . 如果你的更大目标是让绘图和标记分类数据更直接,你应该考虑Seaborn 。 This is a similar question to Scatter plots in Pandas/Pyplot: How to plot by category
这是与Pandas / Pyplot中Scatter图相似的问题:如何按类别绘制
A way to accomplish your goal is to use pandas with labeled columns. 实现目标的一种方法是使用带有标记列的pandas。 Once you have data in a Pandas Dataframe, you can use Seaborn pairplot to make this sort of plot.
在Pandas Dataframe中获得数据后,您可以使用Seaborn配对图来制作此类图。 (Seaborn also has the iris dataset available as a labeled DataFrame)
(Seaborn还提供虹膜数据集作为标记的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)
If you really want to use the sklearn data dict, you can pull that into a dataframe like so: 如果你真的想使用sklearn数据字典,你可以将其拉入数据帧,如下所示:
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.