matplotlib颜色栏具有随机阴影,如何将其删除?

时间:2019-03-06 14:36:00

标签: python matplotlib colorbar

我在图的左侧有一个颜色图(这是一个主要问题,它本身似乎只支持右侧和顶部),没有边框或刻度。

渲染时,它看起来像是一个小阴影。这似乎是基于彩条中间的颜色,并且它根据彩条的厚度而改变位置,并且如果彩条的厚度超过几毫米,它似乎变得不可见。

示例代码:

from mpl_toolkits.axes_grid1 import make_axes_locatable

fig1 = plt.figure()
ax = fig1.subplots()

#Gradient bar
divider = make_axes_locatable(ax)
cax = divider.append_axes("left", size=0.1, pad=0.5)

X = [[.6, .6], [.7, .7]]
im = cax.imshow(X, interpolation='bicubic', aspect='auto')
cb = fig1.colorbar(im, cax=cax, orientation='vertical')
cb.outline.set_visible(False)
for position, spine in cax.spines.items():
    spine.set_visible(False)
cax.get_xaxis().set_ticks([])
cax.get_yaxis().set_ticks([])

plt.tight_layout()

plt.show()

#from io import BytesIO
#f = BytesIO()
#plt.savefig(f, format="svg")

#import xml.etree.cElementTree as ET

#tree, xmlid = ET.XMLID(f.getvalue())

#fn = "DebugTest" + ".svg"
#ET.ElementTree(tree).write(fn)

我包含了svg输出代码,因为可以在图像查看器或浏览器中查看它,从而可以进行缩放。

效果似乎还取决于显示窗口的分辨率和大小。

这是某种渲染伪像吗?我该如何摆脱呢?

image: top of bar

在我的实际项目中,我将svgs嵌入到html和pdf文档中,在这些阴影中,它们看起来不像是像素伪像,您可以进行长距离缩放,并且变得非常明显:

image: zoomed pdf

1 个答案:

答案 0 :(得分:0)

正如评论中提到的那样,问题在于imshow()命令显示的图像有时与随后放置在其上的颜色栏不完全对齐。这是直接从stackoverflow答案此处复制的:Can I place a vertical colorbar to the left of the plot in matplotlib?

评论中建议的解决方案的用处不大,但我最终想出了如何在不显示图像的情况下创建图像的方法:

X = [[.6, .6], [.7, .7]]
im = image.AxesImage(None, interpolation='bicubic')
im.set_array(X)
cb = fig1.colorbar(im, cax=cax, orientation='vertical')