Closed
Description
I am trying to plot some curves with variable width and opacity. To do this I first plot a background image with the correct color, which I then clip using patches.
It works perfectlty, except when I try to export the result as a PDF, where all clipping information is lost if I try to draw more than one curve. There is no problem when exporting to PNG or with the inline viewer.
Code for reproduction
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
fig, ax = plt.subplots()
X = np.linspace(0, 1, 100)
# green curve, rgba = (0, 1, 0, x)
im1 = ax.imshow([np.array((np.zeros_like(X), np.ones_like(X), np.zeros_like(X), X)).transpose()],
aspect=0.5, extent=[0, 1, 0, 1.5])
paths1 = ax.fill_between(X, X**2, X**2+0.5, facecolor='none', lw=2)
patch1 = PathPatch(paths1._paths[0], visible=False)
ax.add_patch(patch1)
im1.set_clip_path(patch1)
# red curve, rgba = (1, 0, 0, x)
im2 = ax.imshow([np.array((np.ones_like(X), np.zeros_like(X), np.zeros_like(X), X)).transpose()],
aspect=0.5, extent=[0, 1, 0, 1.5])
paths2 = ax.fill_between(X, 1.5-X**2, 1-X**2, facecolor='none', lw=2)
patch2 = PathPatch(paths2._paths[0], visible=False)
ax.add_patch(patch2)
im2.set_clip_path(patch2)
fig.savefig('test.pdf')
fig.savefig('test.png')
If I only try to plot a single curve, then everything works as expected.
Is this linked to a known issue?
Does anyone have a workaround?
Many thanks.
Matplotlib version
- Operating system: Windows 10 or Cygwin (first discovered on a SageMath install, but reproduced in a pure python environment)
- Matplotlib version: 3.3.1 and 2.2.4
- Matplotlib backend: module://ipykernel.pylab.backend_inline
- Python version: 3.8.5 and 3.7.3
- Jupyter version (if applicable): jupyterlab 2.2.6 and 2.2.0