matplotlib colorbar有随机阴影,我怎么能删除它?

问题描述 投票:-1回答:1

我在图的左侧有一个色彩图(这本身就是一个主要问题,它似乎只是正确而顶部通常得到支持),没有边框或刻度。

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

示例代码:

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

python matplotlib colorbar
1个回答
0
投票

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

评论中建议的解决方案不太有用,但我最终还是研究了如何在没有imshow的情况下创建图像:

X = [[.6, .6], [.7, .7]]
im = image.AxesImage(None, interpolation='bicubic')
im.set_array(X)
cb = fig1.colorbar(im, cax=cax, orientation='vertical')
© www.soinside.com 2019 - 2024. All rights reserved.