Matplotlib:添加自定义颜色栏,该颜色栏从全透明到全彩色(删除工件) [英] Matplotlib: Add a custom colorbar that runs from full transparent to full color (remove artifacts)

查看:64
本文介绍了Matplotlib:添加自定义颜色栏,该颜色栏从全透明到全彩色(删除工件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的matplotlib图形添加自定义颜色条,该颜色条从全透明(白色)到全彩色(中等紫色).

I am trying to add a custom colorbar to my matplotlib figure, that runs from full transparent (white) to full color (mediumpurple).

我走的很远,但仍然有一些问题.我这样做的方式是在每个补丁之间创建可见的线.当我尝试使颜色条看起来流畅时,这会产生伪影.

I have come far, but still a few issues. The way I make this, creates lines between each patch, which are visible. This creates artifacts when I try to make the colorbar look fluent.

fig, ax = plt.subplots(figsize=(10, 10))
max_val = 4
transparency_ticks = 500
color = mpl.colors.to_rgba(color)
cmap = mpl.colors.ListedColormap([(*color[:3], (1+a)/transparency_ticks) for a in range(transparency_ticks)])
norm = mpl.colors.Normalize(vmin=0, vmax=max_val)
cax = fig.add_axes([0.8, 0.17, 0.05, 0.5])
mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm, orientation='vertical')

这是 transparency_ticks = 500 的图片.这样您就可以看到每个补丁之间的界线.

This is the image for transparency_ticks = 500. So you can see the lines between each patch.

这是 transparency_ticks = 5000 的图片.您已经看不到这些线条了,因为它们已经与其他线条融合在一起了,但是这些线条使颜色栏看起来更暗了.

This is the image for transparency_ticks = 5000. You don't see the lines anymore since they have blended with the rest, but these lines make the colorbar look a lot darker.

推荐答案

而不是 RGBA 上的级别,请使用具有各种饱和度的 HSV :

Instead of levels onRGBA, use HSV with various saturation:

fig, ax = plt.subplots(figsize=(10, 10))
max_val = 4
transparency_ticks = 50
colors = [mpl.colors.hsv_to_rgb((0.83, a/transparency_ticks, 1)) 
          for a in range(transparency_ticks)]
cmap = mpl.colors.ListedColormap(colors)
norm = mpl.colors.Normalize(vmin=0, vmax=max_val)
cax = fig.add_axes([0.8, 0.17, 0.05, 0.5])
mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm, orientation='vertical')

这篇关于Matplotlib:添加自定义颜色栏,该颜色栏从全透明到全彩色(删除工件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆