matplotlib-如果某些数据不可用的话,自定义cmap不符合图形颜色
我有以下代码:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xticks(weightdecay)
ax.set_yticks(learningrate)
ax.set_zticks(trainbatchsize)
arr = numpy.array(f1)
new_col = arr.copy()
new_col[arr < 0.5] = 0
new_col[(arr >= 0.5) & (arr < 0.75)] = 1
new_col[(arr >= 0.75) & (arr < 0.8)] = 2
new_col[(arr >= 0.8) & (arr < 0.85)] = 3
new_col[arr >= 0.85] = 4
new_col = new_col / new_col.max()
cmap = ListedColormap(["magenta", "green", "blue", "orange", "red"])
scat_plot = ax.scatter(xs=weightdecay, ys=learningrate, zs=trainbatchsize, c=new_col, cmap=cmap)
cb = fig.colorbar(scat_plot, pad=0.2)
cb.ax.set_yticklabels([0, 0.5, 0.75, 0.80, 0.85, 1])
但是,颜色杆中的颜色与图中的颜色不同。这是由于以下事实:在我的某些数据集中,一些与特定颜色相对应的数据未定义。关于如何解决这个问题的任何想法。因此,即使缺少某些颜色表示的数据,我也希望始终具有相同的颜色键。
I have the following code:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xticks(weightdecay)
ax.set_yticks(learningrate)
ax.set_zticks(trainbatchsize)
arr = numpy.array(f1)
new_col = arr.copy()
new_col[arr < 0.5] = 0
new_col[(arr >= 0.5) & (arr < 0.75)] = 1
new_col[(arr >= 0.75) & (arr < 0.8)] = 2
new_col[(arr >= 0.8) & (arr < 0.85)] = 3
new_col[arr >= 0.85] = 4
new_col = new_col / new_col.max()
cmap = ListedColormap(["magenta", "green", "blue", "orange", "red"])
scat_plot = ax.scatter(xs=weightdecay, ys=learningrate, zs=trainbatchsize, c=new_col, cmap=cmap)
cb = fig.colorbar(scat_plot, pad=0.2)
cb.ax.set_yticklabels([0, 0.5, 0.75, 0.80, 0.85, 1])
However, the colors within the color-bar are not the same as the colors within the graph. This is due to the fact that within some of my data-sets, some data corresponding to a specific color are not defined. Any idea on how to solve this. So I would like to always have the same color-bar even if data represented by some colors is missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设问题在于
listedColorMap
的使用情况,您是否尝试过linearsemegendegegendedColorMap
(可以在 docs )?I'd assume the problem lies in the usage of
ListedColormap
, did you tryLinearSegmentedColormap
(an example tutorial can be found in the docs)?我建议直接使用颜色序列。
根据 documentation 参数可能是:
“长度为n” 的颜色序列。
因此,您可以使用类似的内容:
在这种情况下,创建配色栏而无需明确的CMAP会更加棘手,但您可以添加一个简单的传奇。
I would recommend using the color sequence directly.
According to the documentation, the
c
argument can be:"A sequence of colors of length n".
So you can use something like:
In this case, it will be more tricky to create the colorbar without explicit cmap yet you can add a simple legend.