matplotlib-如果某些数据不可用的话,自定义cmap不符合图形颜色

发布于 2025-02-01 23:10:06 字数 998 浏览 1 评论 0原文

我有以下代码:

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. enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清眉祭 2025-02-08 23:10:06

我假设问题在于listedColorMap的使用情况,您是否尝试过linearsemegendegegendedColorMap(可以在 docs )?

I'd assume the problem lies in the usage of ListedColormap, did you try LinearSegmentedColormap (an example tutorial can be found in the docs)?

久而酒知 2025-02-08 23:10:06

我建议直接使用颜色序列。
根据 documentation 参数可能是:
“长度为n” 的颜色序列。

因此,您可以使用类似的内容:

colors = ["magenta", "green", "blue", "orange", "red"]
bins = [0.5, 0.75, 0.8, 0.85]
color_indices = numpy.digitize(f1, bins)
c = [colors[i] for i in color_indices]

在这种情况下,创建配色栏而无需明确的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:

colors = ["magenta", "green", "blue", "orange", "red"]
bins = [0.5, 0.75, 0.8, 0.85]
color_indices = numpy.digitize(f1, bins)
c = [colors[i] for i in color_indices]

In this case, it will be more tricky to create the colorbar without explicit cmap yet you can add a simple legend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文