matplotlib / matplotlib/basemap
Flawed PDF save when using linewidth=0 in drawparallels() and drawmeridians()
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 817
- Forks
- 395
- PR merge metrics
- No merged PRs in 30d
Description
Hi all,
I noticed that when saving a basemap plot to a PDF file, the file is flawed when the code has
drawparallels(..., linewidth=0) or drawmerdians(..., linewidth=0) in it.
A minimal working example:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
figure=plt.figure(figsize=(14,7),dpi=100)
for ii in range(4):
axii=figure.add_subplot(2,2,ii+1)
m = Basemap(projection='cyl',
llcrnrlat=40,urcrnrlat=47,\
llcrnrlon=-72,urcrnrlon=-67,resolution='l',
ax=axii, fix_aspect=False)
m.drawcoastlines()
lat_ticks=np.arange(41, 47)
lon_ticks=np.arange(-72, -67)
m.drawparallels(lat_ticks,labels=[1,0,0,0],linewidth=0)
m.drawmeridians(lon_ticks,labels=[0,0,0,1],linewidth=0)
figure.savefig('test.pdf')
The PDF won't display correctly in some PDF viewers, for instance Xreader (v.2.4.4) or Adobe reader. But works fine in others, like Okular or Foxit reader. When compiling a LaTeX document using pdflatex, the page containing that figure won't display properly, again it is PDF viewer dependent.
Here is a screenshot of the PDF file opened in Xreader:

Here is the same file opened in Okular:

The reason for setting linewidth=0 is to remove those grid lines, one relevant questions on Stackoverflow.
One workaround I found is to use zorder=-2 to hide those grid lines while avoiding creating corrupted PDF files.
Note that I've tried a few different matplotlib backends, including Qt5Agg, TkAgg, Agg, and the results are all the same.
Some specs and package versions:
OS: Manjaro Linux
Python = 2.7 or 3.7, in an anaconda env
basemap = 1.2.0
matplotlib = 3.1.3 (for py3) and 1.2.0 (for py2)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the minimal example and inspecting the drawparallels() and drawmeridians() entry points involved in PDF output. Compare the generated PDF in the affected viewers and confirm that using linewidth=0 no longer produces flawed rendering while the grid lines remain hidden.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- matplotlib, python
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100