matplotlib / matplotlib

matplotlib: plotting with Python
https://matplotlib.org/stable/
20.36k stars 7.67k forks source link

tilted line visible in generated pdf file #5948

Closed twmr closed 8 years ago

twmr commented 8 years ago
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np

xr = np.linspace(-10, 10, 2)
X, Y = np.meshgrid(xr, xr)

# ncols needs to be >= 3 to trigger the problem
fig, axes = plt.subplots(nrows=1, ncols=3, sharey=True)

for iax, ax in enumerate(axes.flatten()):
    ax.set_aspect('equal')
    r = Rectangle((X.min(), Y.min()), X.ptp(), Y.ptp(),
                  color='k', fill=False, ls='--', lw=1)
    ax.add_patch(r)
    ax.set_xlim(X.min()-1, X.max()+1)
    ax.set_ylim(Y.min()-1, Y.max()+1)
plt.savefig('export.pdf')
plt.show()

When saving the above figure to a pdf file, an additional (unwanted) tilted dashed line is drawn on the third axis (=axes[2]). Note that this line is only visible in the generated pdf file and not in the interactive window. mplpdfbug

tacaswell commented 8 years ago

The other odd thing is that the x limits are not [-11, 11] like they should be set to...

tacaswell commented 8 years ago

and changing the figure size can suppress this issue as well.

tacaswell commented 8 years ago

It looks like this is an issue with the clipping, these are the paths for the rectangles from left to right (sorry about my silly variable names).

In [7]: print(aardvark_0.decode('ascii'))
-19.44385 58.909091 m
-1 58.909091 m
294.737968 58.909091 l
294.737968 373.090909 l
-1 373.090909 l

In [8]: print(aardvark_1.decode('ascii'))
138.109091 58.909091 m
452.290909 58.909091 l
452.290909 373.090909 l
138.109091 373.090909 l
138.109091 58.909091 l
h

In [9]: print(aardvark_2.decode('ascii'))
295.662032 58.909091 m
577 58.909091 l
577 373.090909 m
295.662032 373.090909 l
295.662032 58.909091 l
h
tacaswell commented 8 years ago

Ah, the problem is the trailing 'h' or the second 'm' in the last rectangle. Removing the h or changing the second m -> l fixes the problem.