# Artifacts when saving as PDF

Getting some strange artifacts when I save a figure as a PDF in
matplotlib. Here are some screen shots. PDF and PNG . 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()
``````
···
``````
Doctoral Student
Dept of Physics and Astronomy
Texas A&M University
``````

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(-(x2 + y2))
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 <boada@…3847…> wrote:

``````Getting some strange artifacts when I save a figure as a PDF in
``````

matplotlib. Here are some screen shots. PDF and PNG . 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 >
``````
1. & (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()
``````
``````
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

source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF_______________________________________________

Matplotlib-users mailing list

Matplotlib-users@…1735…sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

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

···

On 4/6/15 8:47 PM, Thomas Caswell wrote:

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()

--

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
Matplotlib-users@lists.sourceforge.net
<mailto:Matplotlib-users@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--

Doctoral Student
Dept of Physics and Astronomy
Texas A&M University

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

···

On Tue, Apr 7, 2015 at 11:02 AM Steven Boada <boada@…3847…> wrote:

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

On 4/6/15 8:47 PM, Thomas Caswell wrote:

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(-(x2 + y2))

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](http://imgur.com/oQDXkWn)> and PNG <[http://imgur.com/bCw3Fn4](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
``````
``````boada@...3847...  <mailto:boada@...3847...>
``````
``````------------------------------------------------------------------------------
``````
``````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-](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
``````
``````Matplotlib-users@...1735...sourceforge.net
``````
``````<mailto:Matplotlib-users@lists.sourceforge.net>
``````
``````[https://lists.sourceforge.net/lists/listinfo/matplotlib-users](https://lists.sourceforge.net/lists/listinfo/matplotlib-users)
``````

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

source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF

Matplotlib-users mailing list

Matplotlib-users@…1735…sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Tom,

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

···

On 4/7/15 10:39 AM, Thomas Caswell wrote:

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

On 4/6/15 8:47 PM, Thomas Caswell wrote:
> 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()
>
> –
>
>
> 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
> Matplotlib-users@lists.sourceforge.net
<mailto:Matplotlib-users@lists.sourceforge.net>
> <mailto:Matplotlib-users@lists.sourceforge.net
<mailto:Matplotlib-users@lists.sourceforge.net>>
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>

--

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
Matplotlib-users@lists.sourceforge.net
<mailto:Matplotlib-users@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--