Discussion:
[Matplotlib-users] Artifacts when saving as PDF
Steven Boada
2015-04-06 18:33:14 UTC
Permalink
Getting some strange artifacts when I save a figure as a PDF in
matplotlib. Here are some screen shots. PDF <http://imgur.com/oQDXkWn>
and PNG <http://imgur.com/bCw3Fn4>. Any idea why that is happening?

Here is (most of) the source code that makes the plot. I stripped out
the data generation, because it is long and involved, and doesn't really
matter. Basically what the script is supposed to do is make a scatter
plot where the density is below some threshold, and a 2d histogram when
it is above that threshold. The code seems to work fine, but when I save
the figure (using savefig in Ipython) it shows up funny.

Thanks.

import pylab as pyl

bins = [50,50]
thresh = 3

xdat = #generate or load some data
ydat = #generate or load some data

hh, locx, locy = pyl.histogram2d(xdat, ydat,
range=[[-1,4],[-26,-10]], bins=bins)
posx = pyl.digitize(xdat, locx)
posy = pyl.digitize(ydat, locy)

# finds the bins which contain points. posx = 0 for points outside
"range"
ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
# values of histogram with points in the bins.
hhsub = hh[posx[ind] - 1, posy[ind] - 1]

xdat1 = xdat[ind][hhsub < thresh] # low density points
ydat1 = ydat[ind][hhsub < thresh]
hh[hh < thresh] = pyl.nan # fill the areas with low density by NaNs

pyl.scatter(xdat1, ydat1, s=20, c='0.8')
pyl.imshow(pyl.log10(hh.T), cmap='gray_r',
extent=pyl.array([[-1,4],[-26,-10]]).flatten(),
interpolation='none')

pyl.show()
--
Steven Boada

Doctoral Student
Dept of Physics and Astronomy
Texas A&M University
***@physics.tamu.edu
Thomas Caswell
2015-04-07 01:47:28 UTC
Permalink
This is probaly due to issues with not all of the vector backends
supporting alpha gracefully.

This can be reproduced more simply by

x, y = np.ogrid[-5:5:.1, -5:5:.1]
dd = np.exp(-(x**2 + y**2))
dd[dd < .1] = np.nan

fig, ax = plt.subplots()
ax.imshow(dd, interpolation='none', cmap='gray_r')
plt.savefig('test.pdf')

@steven In the future it is best to report bugs with minimal copy-paste
able examples.
Post by Steven Boada
Getting some strange artifacts when I save a figure as a PDF in
matplotlib. Here are some screen shots. PDF <http://imgur.com/oQDXkWn>
and PNG <http://imgur.com/bCw3Fn4>. Any idea why that is happening?
Here is (most of) the source code that makes the plot. I stripped out the
data generation, because it is long and involved, and doesn't really
matter. Basically what the script is supposed to do is make a scatter plot
where the density is below some threshold, and a 2d histogram when it is
above that threshold. The code seems to work fine, but when I save the
figure (using savefig in Ipython) it shows up funny.
Thanks.
import pylab as pyl
bins = [50,50]
thresh = 3
xdat = #generate or load some data
ydat = #generate or load some data
hh, locx, locy = pyl.histogram2d(xdat, ydat, range=[[-1,4],[-26,-10]],
bins=bins)
posx = pyl.digitize(xdat, locx)
posy = pyl.digitize(ydat, locy)
# finds the bins which contain points. posx = 0 for points outside
"range"
ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
# values of histogram with points in the bins.
hhsub = hh[posx[ind] - 1, posy[ind] - 1]
xdat1 = xdat[ind][hhsub < thresh] # low density points
ydat1 = ydat[ind][hhsub < thresh]
hh[hh < thresh] = pyl.nan # fill the areas with low density by NaNs
pyl.scatter(xdat1, ydat1, s=20, c='0.8')
pyl.imshow(pyl.log10(hh.T), cmap='gray_r',
extent=pyl.array([[-1,4],[-26,-10]]).flatten(),
interpolation='none')
pyl.show()
--
Steven Boada
Doctoral Student
Dept of Physics and Astronomy
------------------------------------------------------------
------------------
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM through live
exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-
event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_
campaign=VA_SF_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Steven Boada
2015-04-07 15:01:25 UTC
Permalink
Thomas,

Thanks for the smaller example. I would have come up with one, but I
wasn't sure what was causing it to begin with.

Is there anything to be done to prevent this? Just use another backend?

Steven
Post by Thomas Caswell
This is probaly due to issues with not all of the vector backends
supporting alpha gracefully.
This can be reproduced more simply by
x, y = np.ogrid[-5:5:.1, -5:5:.1]
dd = np.exp(-(x**2 + y**2))
dd[dd < .1] = np.nan
fig, ax = plt.subplots()
ax.imshow(dd, interpolation='none', cmap='gray_r')
plt.savefig('test.pdf')
@steven In the future it is best to report bugs with minimal
copy-paste able examples.
Getting some strange artifacts when I save a figure as a PDF in
matplotlib. Here are some screen shots. PDF
<http://imgur.com/oQDXkWn> and PNG <http://imgur.com/bCw3Fn4>. Any
idea why that is happening?
Here is (most of) the source code that makes the plot. I stripped
out the data generation, because it is long and involved, and
doesn't really matter. Basically what the script is supposed to do
is make a scatter plot where the density is below some threshold,
and a 2d histogram when it is above that threshold. The code seems
to work fine, but when I save the figure (using savefig in
Ipython) it shows up funny.
Thanks.
import pylab as pyl
bins = [50,50]
thresh = 3
xdat = #generate or load some data
ydat = #generate or load some data
hh, locx, locy = pyl.histogram2d(xdat, ydat,
range=[[-1,4],[-26,-10]], bins=bins)
posx = pyl.digitize(xdat, locx)
posy = pyl.digitize(ydat, locy)
# finds the bins which contain points. posx = 0 for points
outside "range"
ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
# values of histogram with points in the bins.
hhsub = hh[posx[ind] - 1, posy[ind] - 1]
xdat1 = xdat[ind][hhsub < thresh] # low density points
ydat1 = ydat[ind][hhsub < thresh]
hh[hh < thresh] = pyl.nan # fill the areas with low density by NaNs
pyl.scatter(xdat1, ydat1, s=20, c='0.8')
pyl.imshow(pyl.log10(hh.T), cmap='gray_r',
extent=pyl.array([[-1,4],[-26,-10]]).flatten(),
interpolation='none')
pyl.show()
--
Steven Boada
Doctoral Student
Dept of Physics and Astronomy
Texas A&M University
------------------------------------------------------------------------------
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM through live
exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-
event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
--
Steven Boada

Doctoral Student
Dept of Physics and Astronomy
Texas A&M University
***@physics.tamu.edu
Thomas Caswell
2015-04-07 15:39:05 UTC
Permalink
This probably should be made into an issue on github as this is clearly a
bug.

On further consideration, the fact that in my example the bad pixels show
up only on the edge and are not symmetric makes me think that my original
suggestion is wrong. Does `interpoltation='nearest'` work any better?

I also am not sure that the rasterzation is happening so this may be an
issue on the renderer end.

This needs more investigation that I have time for today.

Tom
Post by Steven Boada
Thomas,
Thanks for the smaller example. I would have come up with one, but I
wasn't sure what was causing it to begin with.
Is there anything to be done to prevent this? Just use another backend?
Steven
Post by Thomas Caswell
This is probaly due to issues with not all of the vector backends
supporting alpha gracefully.
This can be reproduced more simply by
x, y = np.ogrid[-5:5:.1, -5:5:.1]
dd = np.exp(-(x**2 + y**2))
dd[dd < .1] = np.nan
fig, ax = plt.subplots()
ax.imshow(dd, interpolation='none', cmap='gray_r')
plt.savefig('test.pdf')
@steven In the future it is best to report bugs with minimal
copy-paste able examples.
Getting some strange artifacts when I save a figure as a PDF in
matplotlib. Here are some screen shots. PDF
<http://imgur.com/oQDXkWn> and PNG <http://imgur.com/bCw3Fn4>. Any
idea why that is happening?
Here is (most of) the source code that makes the plot. I stripped
out the data generation, because it is long and involved, and
doesn't really matter. Basically what the script is supposed to do
is make a scatter plot where the density is below some threshold,
and a 2d histogram when it is above that threshold. The code seems
to work fine, but when I save the figure (using savefig in
Ipython) it shows up funny.
Thanks.
import pylab as pyl
bins = [50,50]
thresh = 3
xdat = #generate or load some data
ydat = #generate or load some data
hh, locx, locy = pyl.histogram2d(xdat, ydat,
range=[[-1,4],[-26,-10]], bins=bins)
posx = pyl.digitize(xdat, locx)
posy = pyl.digitize(ydat, locy)
# finds the bins which contain points. posx = 0 for points
outside "range"
ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
# values of histogram with points in the bins.
hhsub = hh[posx[ind] - 1, posy[ind] - 1]
xdat1 = xdat[ind][hhsub < thresh] # low density points
ydat1 = ydat[ind][hhsub < thresh]
hh[hh < thresh] = pyl.nan # fill the areas with low density by NaNs
pyl.scatter(xdat1, ydat1, s=20, c='0.8')
pyl.imshow(pyl.log10(hh.T), cmap='gray_r',
extent=pyl.array([[-1,4],[-26,-10]]).flatten(),
interpolation='none')
pyl.show()
--
Steven Boada
Doctoral Student
Dept of Physics and Astronomy
Texas A&M University
------------------------------------------------------------
------------------
Post by Thomas Caswell
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM through live
exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-
event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_
campaign=VA_SF_______________________________________________
Post by Thomas Caswell
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
--
Steven Boada
Doctoral Student
Dept of Physics and Astronomy
Texas A&M University
------------------------------------------------------------
------------------
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM through live
exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-
event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Steven Boada
2015-04-07 16:18:26 UTC
Permalink
Hi Tom,

Thanks for your help.

interpolation='nearest' doesn't produce any problems. I'm currently
using TkAgg, and I checked with a buddy of mine, using MacOSX backend.
Neither of us see any problems using interpolation='nearest'. He is
using an older version of MPL which doesn't let him use
interpolation='none'.

I will ask around to see if anyone can run a few more test cases.

Steven
Post by Thomas Caswell
This probably should be made into an issue on github as this is
clearly a bug.
On further consideration, the fact that in my example the bad pixels
show up only on the edge and are not symmetric makes me think that my
original suggestion is wrong. Does `interpoltation='nearest'` work
any better?
I also am not sure that the rasterzation is happening so this may be
an issue on the renderer end.
This needs more investigation that I have time for today.
Tom
Thomas,
Thanks for the smaller example. I would have come up with one, but I
wasn't sure what was causing it to begin with.
Is there anything to be done to prevent this? Just use another backend?
Steven
Post by Thomas Caswell
This is probaly due to issues with not all of the vector backends
supporting alpha gracefully.
This can be reproduced more simply by
x, y = np.ogrid[-5:5:.1, -5:5:.1]
dd = np.exp(-(x**2 + y**2))
dd[dd < .1] = np.nan
fig, ax = plt.subplots()
ax.imshow(dd, interpolation='none', cmap='gray_r')
plt.savefig('test.pdf')
@steven In the future it is best to report bugs with minimal
copy-paste able examples.
On Mon, Apr 6, 2015 at 5:41 PM Steven Boada
Getting some strange artifacts when I save a figure as a PDF in
matplotlib. Here are some screen shots. PDF
<http://imgur.com/oQDXkWn> and PNG
<http://imgur.com/bCw3Fn4>. Any
Post by Thomas Caswell
idea why that is happening?
Here is (most of) the source code that makes the plot. I
stripped
Post by Thomas Caswell
out the data generation, because it is long and involved, and
doesn't really matter. Basically what the script is supposed
to do
Post by Thomas Caswell
is make a scatter plot where the density is below some
threshold,
Post by Thomas Caswell
and a 2d histogram when it is above that threshold. The code
seems
Post by Thomas Caswell
to work fine, but when I save the figure (using savefig in
Ipython) it shows up funny.
Thanks.
import pylab as pyl
bins = [50,50]
thresh = 3
xdat = #generate or load some data
ydat = #generate or load some data
hh, locx, locy = pyl.histogram2d(xdat, ydat,
range=[[-1,4],[-26,-10]], bins=bins)
posx = pyl.digitize(xdat, locx)
posy = pyl.digitize(ydat, locy)
# finds the bins which contain points. posx = 0 for points
outside "range"
ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <=
bins[1])
# values of histogram with points in the bins.
hhsub = hh[posx[ind] - 1, posy[ind] - 1]
xdat1 = xdat[ind][hhsub < thresh] # low density points
ydat1 = ydat[ind][hhsub < thresh]
hh[hh < thresh] = pyl.nan # fill the areas with low
density by
Post by Thomas Caswell
NaNs
pyl.scatter(xdat1, ydat1, s=20, c='0.8')
pyl.imshow(pyl.log10(hh.T), cmap='gray_r',
extent=pyl.array([[-1,4],[-26,-10]]).flatten(),
interpolation='none')
pyl.show()
--
Steven Boada
Doctoral Student
Dept of Physics and Astronomy
Texas A&M University
------------------------------------------------------------------------------
Post by Thomas Caswell
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM
through live
Post by Thomas Caswell
exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-
event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF_______________________________________________
Post by Thomas Caswell
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
--
Steven Boada
Doctoral Student
Dept of Physics and Astronomy
Texas A&M University
------------------------------------------------------------------------------
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM through live
exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-
event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
--
Steven Boada

Doctoral Student
Dept of Physics and Astronomy
Texas A&M University
***@physics.tamu.edu
Jouni K Seppänen
2015-04-13 05:36:46 UTC
Permalink
Thanks for the report, I turned it into a github issue:
https://github.com/matplotlib/matplotlib/issues/4331

Continue reading on narkive:
Loading...