Rasterize Poly3DCollection in Matplotlib

When trying to force rasterization of an object Poly3DCollectionin Matplotlib, the following error message appears (and I can confirm that rasterization is not applied):

/usr/lib/python3/dist-packages/matplotlib/artist.py:788: UserWarning: Rasterization of '<mpl_toolkits.mplot3d.art3d.Poly3DCollection object at 0x2b49e8faeba8>' will be ignored
  warnings.warn("Rasterization of '%s' will be ignored" % self)

You can rasterize the entire figure, but it is obviously preferable to rasterize some objects, preserving elements such as axes, labels, key, text, etc. as vector graphics.

I tried using the following syntax in my code:

  • ax.add_collection3d(Poly3DCollection(polygons, rasterized=True), zs='z')
  • c = Poly3DCollection(polygons), and then c.set_rasterized(True)

According to this message, you can rasterize an object PolyCollection(without 3D bits).

Any suggestions?

Here is the MWE:

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.set_xlim([0, 4])
ax.set_ylim([0, 4])
ax.set_zlim([0, 4])

polygons = [[(3, 1, 1), (1, 3, 1), (1, 1, 3)]]

ax.add_collection3d(Poly3DCollection(polygons, facecolors='r', rasterized=True), zs='z')

plt.show()
+4
source share
1 answer

, , , pdf. , , , , - plt.savefig("some.pdf").

, - .

, matplotlib,

python\lib\site-packages\mpl_toolkits\mplot3d\art3d.py

from matplotlib.artist import allow_rasterization

Poly3DCollection draw ( 709) @allow_rasterization.

@allow_rasterization
def draw(self, renderer):
    return Collection.draw(self, renderer)

pdf, . pdf:

enter image description here

+2

Source: https://habr.com/ru/post/1690798/


All Articles