简体   繁体   English

如何为 patchcollection 设置 edgecolor 的颜色限制属性(python、matplotlib)

[英]How to set color limits property of edgecolor for a patchcollection (python, matplotlib)

I am plotting volumetric distributions with x, y and z axis (particle size, particle shape, volume (%) (z-axis as colour scale). Upon exporting the figure as svg, white space appears between the plotted rectangles. I therefore tried setting the edgecolor of the patchcollection. This works, however, the edgecolors have different clim and therefore the color of the edges differs from the patches themselves (see example figure). Code for setting the patchcollection:我正在用 x、y 和 z 轴(颗粒大小、颗粒形状、体积(%)(z 轴作为色标)绘制体积分布。将图形导出为 svg 后,绘制的矩形之间会出现空白。因此我尝试了设置 patchcollection 的 edgecolor。然而,这可行,但 edgecolors 具有不同的clim,因此边缘的颜色与 patch 本身不同(参见示例图)。设置 patchcollection 的代码:

p = PatchCollection(rects,cmap=matplotlib.cm.viridis)
colors = curem # colour of rectangles based on volume of the size-shape bins of curem
p.set_array(np.array(colors)) 
p.set_edgecolor(matplotlib.cm.viridis(np.array(colors))) 
p.set_clim([0, math.ceil(np.max(ems[ii]))]) # set maximum to max Rsqclass rounded up to next 1/5th
axs[zz].add_collection(p) # add all the rectangles to the figure

So how do I go about setting the color limits for the edges?那么我该如何设置边缘的颜色限制呢? Thanks in advance, Hans提前致谢,汉斯在此处输入图片说明

After trying out several of Diziet Asahi's proposed solutions, I found that for this specific case, the following works:在尝试了 Diziet Asahi 提出的几个解决方案后,我发现对于这种特定情况,以下方法有效:

p.set_edgecolor('face') # edgecolor equal to facecolor p.set_linewidth(0.5)

The edgecolor is now equal to the facecolor.边缘颜色现在等于面颜色。 A small linewidth (eg the suggested 0.000000000001) does not work.小线宽(例如建议的 0.000000000001)不起作用。 White lines still appear when the svg is viewed in microsoft internet explorer or inkscape.在 microsoft internet explorer 或inkscape 中查看svg 时,仍会出现白线。 A linewidth of 0.5 gets rid of the wite space. 0.5 的线宽消除了白色空间。 However, it should be noted that the required linewidth depends on the level of zoom.但是,需要注意的是,所需的线宽取决于缩放级别。 Eg, at 400% zoom a linewidth of 0.1 is sufficient.例如,在 400% 缩放时,0.1 的线宽就足够了。 At 30% zoom a linewidth of 0.5 is still insufficient.在 30% 缩放时,0.5 的线宽仍然不够。

One further note: the above solution is specific to svg vector graphics.进一步说明:上述解决方案特定于 svg 矢量图形。 When stored as .pdf, the solution that Diziet linked to actually does work (set linewidth to an infinitesimally small value).当存储为 .pdf 时,Diziet 链接到的解决方案实际上确实有效(将线宽设置为一个无穷小的值)。

There seem to be a general problem with showing undesired linestrokes with vector backends, as per this issue on matplotlib's github根据 matplotlib 的 github 上的这个问题,使用矢量后端显示不需要的线条似乎存在一个普遍问题

A proposed temporary fix is to use very small, but non-zero linewidths:建议的临时修复是使用非常小但非零的线宽:

either pass linewidths=0.000000000001 to the constructor要么将linewidths=0.000000000001传递给构造函数

p = PatchCollection(rects,cmap=matplotlib.cm.viridis, linewidths=0.000000000001)

or use或使用

p.set_linewidth(0.000000000001)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM