1

In my python program I plot some 2D graphs with imshow:

img=imshow(data,
           interpolation='none',
           vmin=0.0001,
           vmax=0.01,
           cmap=custom_map,
           norm=LogNorm()
           )

When I save the plot as *.eps or *.pdf I get some strange lines in the pictures, where the values of data are very close to vmin.

When I save the plot as *.png or *.svg or *.jpg this error doesn't occur.

How can I avoid that python draws this strange lines?

enter image description here

EDIT: Here is a minimal code which should reproduce the problem. At least I get the error all the times...

from matplotlib.colors import LogNorm
import numpy as np
import matplotlib.pyplot as pl

A=np.array([[0.000000,0.000426],[0.000000,0.000524]], dtype=np.float)
pl.imshow(A, interpolation='none', norm=LogNorm())

pl.savefig('test.eps')
pl.savefig('test.png')

Even if you do not use LogNorm() but use np.log(A) the problem occurs in the output.

Edit 2 With

A=np.array([[np.log(0),np.log(0),np.log(0.1),np.log(0.000426)],
        [np.log(0),np.log(0),np.log(0),np.log(0.000524)]], dtype=np.float)

the problem occurs only at the borders from np.log(0) to some real values as you can see in the next picture

enter image description here

FrankTheTank
  • 1,579
  • 4
  • 14
  • 19
  • does your custom map have alpha in it? See https://github.com/matplotlib/matplotlib/issues/2473. From the code you gave here there is no way to reproduce your problem (and hence sort out what your problem is). Please post a _minimal_ amount of code which will reproduce the problem. _Most_ matplotlib problems can be reproduced with < 15 LoC. – tacaswell May 27 '14 at 14:03
  • To be honest, I can't reproduce the problem with any analyticval function which should fit to the shown pattern.... I tried several things: No `vmin` and `vmax`, no `LogNorm`, no `cmap`... the problem occurs only when saving to .eps and .pdf. When the picture is opened in `spyder` after the `pl.show()`-line, there are no black lines. So the error occurs when saving the file!? EDIT: my custom map has no alpha in it. – FrankTheTank May 27 '14 at 14:50
  • The thing you see on the screen is probably rendered through Agg, the ps and pdf paths use two entirely different (vector based) code paths (backends). This is why getting a simple reproducible example is important. Also, what version of mpl are you using? – tacaswell May 27 '14 at 15:07
  • Thank you for your answers. I added a piece of code with which my problem is reproducable... I use version 1.3.1 of mpl and PyCharm as IDE. – FrankTheTank May 28 '14 at 07:52

1 Answers1

0

Seems like this is a problem with LogNorm() and the zeros in the array... So the bad pixels should have a special colorvalue which can be manually set. For the people who are looking for answers: here is a description: How can I plot NaN values as a special color with imshow in matplotlib?

Now change the code in the opening post:

cmap = matplotlib.cm.jet
cmap.set_bad('w',1.)
pl.imshow(A, interpolation='none', cmap=cmap, norm=LogNorm())

With this the eps output is correct - or better: as wanted.

If there are some other ideas, please add them to the comments or answers.

enter image description here

Community
  • 1
  • 1
FrankTheTank
  • 1,579
  • 4
  • 14
  • 19