Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

python - White lines in matplotlib's pcolor

In some pdf viewers such as Preview on OSX, plots made with matplotlib's pcolor have white lines (see image below). How can I get rid of them?

The source code is very simple (choose any data for x,y,z):

import matplotlib
matplotlib.use("pdf")
import matplotlib.pyplot as pl
pl.figure()
pl.pcolormesh(x,y,z,cmap="Blues",linewidth=0) 
pl.savefig("heatmap.pdf")

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The comments have a good solution that utilizes imshow. When imshow is not appropriate for the input data (e.g. it is not uniformly spaced) this generally solves this problem,

pcol = pl.pcolormesh(x,y,z,cmap="Blues",linewidth=0,)
pcol.set_edgecolor('face')

If that approach does not reduce the lines sufficiently, you can also try this:

pl.pcolormesh(x,y,z,cmap="Blues",linewidth=0,rasterized=True)

In addition to reducing the lines between squares this approach also tends to reduce file size a bit, which is sometimes useful. In this case you may want to tune the dpi setting when saving (e.g. pl.savefig("heatmap.pdf", dpi=300)) until you get something that is satisfactory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...