为什么散点图中点的颜色与相应图例中点的颜色不匹配?
Why doesn't the color of the points in a scatter plot match the color of the points in the corresponding legend?
我通过下面的代码通过 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'
可以使用类似于蓝点、紫点、橙红色点和黄点的内容生成图例。尽管颜色条可以作为一种可能的替代方案,但我还没有查看任何关于颜色条的文档。
颜色条可以通过plt.colorbar()
实现。这将允许直接查看与颜色对应的值。
让图例中的点显示不同的颜色当然也很好,虽然它不允许提供任何量化信息。
不幸的是,matplotlib 没有提供任何内置方法来实现这一点。因此,一种方法是子 class 用于创建图例句柄并实现此功能的图例处理程序。
这里我们使用自定义 create_collection
方法创建了一个 ScatterHandler
,在该方法中我们创建了所需的 PathCollection
并通过在 legend_map
字典中指定它来使用它传说。
handler_map={ type(sc) : ScatterHandler()}
下面的代码乍一看似乎有点复杂,但是您可以在没有完全理解的情况下简单地复制class并在您的代码中使用它。
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()
我通过下面的代码通过 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'
可以使用类似于蓝点、紫点、橙红色点和黄点的内容生成图例。尽管颜色条可以作为一种可能的替代方案,但我还没有查看任何关于颜色条的文档。
颜色条可以通过plt.colorbar()
实现。这将允许直接查看与颜色对应的值。
让图例中的点显示不同的颜色当然也很好,虽然它不允许提供任何量化信息。
不幸的是,matplotlib 没有提供任何内置方法来实现这一点。因此,一种方法是子 class 用于创建图例句柄并实现此功能的图例处理程序。
这里我们使用自定义 create_collection
方法创建了一个 ScatterHandler
,在该方法中我们创建了所需的 PathCollection
并通过在 legend_map
字典中指定它来使用它传说。
handler_map={ type(sc) : ScatterHandler()}
下面的代码乍一看似乎有点复杂,但是您可以在没有完全理解的情况下简单地复制class并在您的代码中使用它。
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()