【问题标题】:Why doesn't the color of the points in a scatter plot match the color of the points in the corresponding legend?为什么散点图中点的颜色与相应图例中点的颜色不匹配?
【发布时间】:2018-08-24 03:42:43
【问题描述】:

我通过下面的代码通过 matplotlib 获得了一个示例散点图。

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 100, 501)
y = np.sin(x)

label = 'xy data sample'

plt.scatter(x, y, cmap='plasma', c=x, label=label)
legend_dict = dict(ncol=1, loc='best', scatterpoints=4, fancybox=True, shadow=True)
plt.legend(**legend_dict)
plt.show()

运行上面的代码会产生下面的图。

颜色图已成功绘制,但图例显示的点都是蓝色的,而不是与所选颜色图对应的颜色的点。为什么会这样?

我尝试将cmap='plasma' 放入legend_dict,但会导致以下错误。

File "/Users/.../
site-packages/matplotlib/axes/_axes.py", line 550, in legend
    self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'cmap'

编辑:

我想要的输出是通过选择的颜色图让图例中表示的四个点成为不同的颜色。理想情况下,本例中的cmap='plasma' 可以使用类似于蓝点、紫点、橙红点、黄点的图例生成图例。虽然颜色条可以作为一种可能的替代方案,但我还没有查看任何关于颜色条的文档。

【问题讨论】:

  • 发生这种情况的原因是 matplotlib 就是这样设计的。您希望积分具有其他哪种颜色?请写清楚你想要达到的目标。
  • 颜色条会更适合这个吗?否则,您将有很多图例条目。可能是this 之类的东西?
  • 我更新了帖子来解决这个问题。

标签: python matplotlib legend scatter-plot colormap


【解决方案1】:

可以通过plt.colorbar() 实现颜色条。这将允许直接查看与颜色对应的值。

让图例中的点显示不同的颜色当然也不错,尽管它不允许提供任何定量信息。

不幸的是,matplotlib 没有提供任何内置的方法来实现这一点。因此,一种方法是将用于创建图例句柄的图例处理程序子类化并实现此功能。

这里我们使用自定义create_collection 方法创建ScatterHandler,在该方法中我们创建所需的PathCollection 并通过在图例的legend_map 字典中指定它来使用它。

handler_map={ type(sc) : ScatterHandler()}

下面的代码乍一看有点复杂,但是你可以在不完全理解的情况下简单地复制这个类并在你的代码中使用它。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerRegularPolyCollection

class ScatterHandler(HandlerRegularPolyCollection):
    def update_prop(self, legend_handle, orig_handle, legend):
        legend._set_artist_props(legend_handle)
        legend_handle.set_clip_box(None)
        legend_handle.set_clip_path(None)

    def create_collection(self, orig_handle, sizes, offsets, transOffset):
        p = type(orig_handle)([orig_handle.get_paths()[0]],
                              sizes=sizes, offsets=offsets,
                              transOffset=transOffset,
                              cmap=orig_handle.get_cmap(),
                              norm=orig_handle.norm )

        a = orig_handle.get_array()
        if type(a) != type(None):
            p.set_array(np.linspace(a.min(),a.max(),len(offsets)))
        else:
            self._update_prop(p, orig_handle)
        return p


x = np.linspace(0, 100, 501)
y = np.sin(x)*np.cos(x/50.)

sc = plt.scatter(x, y, cmap='plasma', c=x, label='xy data sample')

legend_dict = dict(ncol=1, loc='best', scatterpoints=4, fancybox=True, shadow=True)
plt.legend(handler_map={type(sc) : ScatterHandler()}, **legend_dict)

plt.show()

【讨论】:

  • 我对课程越来越熟悉了;我发布这个问题是为了将答案合并到一个类中,该类根据输入(而不是手动指定 plt.xlim(...)plt.xlabel(...) 等和各种 kwargs)输出所需类型的图(scatterbar 等)在 labelcolor 等 matplotlib 函数中使用。由于我的意图是让我的绘图类继承你的类(或者以某种方式在我的绘图类中重新定义它),你介意解释一下 ScatterHandler 类 - 特别是关于什么各种类型的句柄是和orig_handle.get_paths()[0]?
  • 我认为您根本不想更改 ScatterHandler。并且继承处理程序类没有意义。 orig_handle.get_paths()[0] 是散点图的形状(本例中为圆形)。
  • 有道理,这种方法更简单。我尝试在plt.bar 例程中使用orig_handle.get_paths()[0] 失败。我很难找到相关文档来调整各种图的图例。你知道我在哪里可以找到相关文档吗?
  • plt.bar() 返回一个BarContainer,它有一个属性patches。策略始终是查看返回类型并在文档中找到它们。
猜你喜欢
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多