栅格化matplotlib轴内容

栅格化matplotlib轴内容

本文介绍了栅格化matplotlib轴内容(但不包括框架,标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一篇文章,我正在生成变形的有限元网格图,我可以使用matplotlib的polycollection对其进行可视化.图像另存为pdf.

For an article I am generating plots of deformed finite element meshes, which I visualize using matplotlib's polycollection. The images are saved as pdf.

高密度网格会出现问题,对于这种网格而言,幼稚的方法会导致文件过大,渲染过于密集以至于无法实用.

Problems arise for high density meshes, for which the naive approach results in files that are too large and rendering too intensive to be practical.

对于这些网格,将每个元素绘制为多边形确实没有任何意义.它很容易栅格化,就像将图像另存为jpg或png一样.但是,对于打印,我想保留清晰的框架,标签和注释.

For these meshes it really makes no sense to plot each element as a polygon; it could easily be rasterized, as is done when saving the image as jpg or png. However, for print I would like to hold on to a sharp frame, labels, and annotations.

有人知道是否可以在matplotlib中实现这种混合栅格化吗?

Does anyone know if it is possible to achieve this kind of hybrid rasterization in matplotlib?

我可以想到涉及imshow并绕过polycollection的解决方案,但我更喜欢使用matplotlib的内置组件.

I can think of solutions involving imshow, and bypassing polycollection, but I would much prefer to use matplotlib's built-in components.

感谢您的建议.

推荐答案

只需将rasterized=True关键字传递给集合构造函数.示例:

Just pass the rasterized=True keyword to your collection constructor. Example:

col = collections.PolyCollection(<arguments>, rasterized=True)

这仅允许对该元素进行选择性栅格化(例如,如果在其上方进行了法线绘制,则默认情况下将对其进行矢量化处理).大多数命令,例如plotimshow,也可以使用rasterized关键字.如果要栅格化整个图形(包括标签和注释),可以这样做:

This allows a selective rasterization of that element only (e.g., if you did a normal plot on top of it, it would be vectorized by default). Most commands like plot or imshow can also take the rasterized keyword. If one wants to rasterize the whole figure (including labels and annotations), this would do it:

fig = plt.figure()
a = fig.add_subplot(1,1,1, rasterized=True)

(但这不是您想要的,如问题中所述.)

(But this is not what you want, as stated in the question.)

这篇关于栅格化matplotlib轴内容(但不包括框架,标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:41