graphic cut off using matplotlib path example

687 views
Skip to first unread message

Bruce Cohen

unread,
Feb 20, 2010, 9:11:38 PM2/20/10
to sage-support
I am trying to use the path object (see http://matplotlib.sourceforge.net/users/path_tutorial.html).
This is code taken from the tutorial (with one change to show the
resulting png file). The code works fine in python, but cuts off
about the bottom fifth of the graphic in Sage. I have published this
at
http://sagenb.org/home/pub/1648/

Thanks.

-Bruce

ps. I am including the code, but I am not sure whether it is better to
just point to the published notebook and cut down on the length of
this post.

## begin code

import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches

verts = [
(0., 0.), # P0
(0.2, 1.), # P1
(1., 0.8), # P2
(0.8, 0.), # P3
]

codes = [Path.MOVETO,
Path.CURVE4,
Path.CURVE4,
Path.CURVE4,
]

path = Path(verts, codes)

fig = plt.figure()
ax = fig.add_subplot(111)
patch = patches.PathPatch(path, facecolor='none', lw=2)
ax.add_patch(patch)

xs, ys = zip(*verts)
ax.plot(xs, ys, 'x--', lw=2, color='black', ms=10)

ax.text(-0.05, -0.05, 'P0')
ax.text(0.15, 1.05, 'P1')
ax.text(1.05, 0.85, 'P2')
ax.text(0.85, -0.05, 'P3')

ax.set_xlim(-0.1, 1.1)
ax.set_ylim(-0.1, 1.1)
#plt.show()
plt.savefig('bezier.png')

## end code


Jason Grout

unread,
Feb 23, 2010, 8:37:48 AM2/23/10
to sage-s...@googlegroups.com
On 02/20/2010 08:11 PM, Bruce Cohen wrote:
> I am trying to use the path object (see http://matplotlib.sourceforge.net/users/path_tutorial.html).
> This is code taken from the tutorial (with one change to show the
> resulting png file). The code works fine in python, but cuts off
> about the bottom fifth of the graphic in Sage. I have published this
> at
> http://sagenb.org/home/pub/1648/
>

Is this the problem referenced in this thread on the matplotlib mailing
list?

http://old.nabble.com/saving-images-using-pure-matplotlib-in-Sage-cuts-off-the-bottom-part-%28and-produces-corrupt-file-%29-tt25702258.html#a25702258

To test this, can you try the following code to save the figure:

f = open("test.png", "wb")
plt.savefig(f, format="png", dpi=50)
f.flush()

Thanks,

Jason

Jason Grout

unread,
Feb 23, 2010, 11:06:01 AM2/23/10
to sage-s...@googlegroups.com
On 02/20/2010 08:11 PM, Bruce Cohen wrote:
> I am trying to use the path object (see http://matplotlib.sourceforge.net/users/path_tutorial.html).
> This is code taken from the tutorial (with one change to show the
> resulting png file). The code works fine in python, but cuts off
> about the bottom fifth of the graphic in Sage. I have published this
> at
> http://sagenb.org/home/pub/1648/
>

The image in the published worksheet looks fine. What exactly do you
mean by "in python" and "in Sage" above?

Thanks,

Jason


Bruce Cohen

unread,
Feb 23, 2010, 5:59:57 PM2/23/10
to sage-support

On Feb 23, 8:06 am, Jason Grout <jason-s...@creativetrax.com> wrote:
> On 02/20/2010 08:11 PM, Bruce Cohen wrote:
>

> > I am trying to use the path object (seehttp://matplotlib.sourceforge.net/users/path_tutorial.html).


> > This is code taken from the tutorial (with one change to show the
> > resulting png file).  The code works fine in python, but cuts off
> > about the bottom fifth of the graphic in Sage.  I have published this
> > at
> >http://sagenb.org/home/pub/1648/
>
> The image in the published worksheet looks fine.  What exactly do you
> mean by "in python" and "in Sage" above?

sagenb is down at the moment, so I can't recheck it. On my local
machine I get the bottom cut off, but when I resave the file,
e.g.
plt.savefig('foo.png',dpi=72)
The new picture looks fine as you say.

Your pointer to
http://old.nabble.com/saving-images-using-pure-matplotlib-in-Sage-cuts-off-the-bottom-part-%28and-produces-corrupt-file-%29-tt25702258.html#a25702258

has been helpful for now.

> ... What exactly do you


> mean by "in python" and "in Sage" above?

I have installed matplotlib as part of a vanilla python system at the
unix command line e.g.
[herstein-2:~] 4% python
Python 2.6.1 (r261:67515, Dec 6 2008, 16:42:21)
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin


Thanks.

-Bruce


Bruce Cohen

unread,
Feb 24, 2010, 1:04:29 AM2/24/10
to sage-support

On Feb 23, 2:59 pm, Bruce Cohen <math.co...@gmail.com> wrote:
> On Feb 23, 8:06 am, Jason Grout <jason-s...@creativetrax.com> wrote:
>
> > On 02/20/2010 08:11 PM, Bruce Cohen wrote:
>
> > > I am trying to use the path object (seehttp://matplotlib.sourceforge.net/users/path_tutorial.html).
> > > This is code taken from the tutorial (with one change to show the
> > > resulting png file).  The code works fine in python, but cuts off
> > > about the bottom fifth of the graphic in Sage.  I have published this
> > > at
> > >http://sagenb.org/home/pub/1648/
>
> > The image in the published worksheet looks fine.  What exactly do you
> > mean by "in python" and "in Sage" above?
>
> sagenb is down at the moment, so I can't recheck it.  

sagenb is back, and the worksheet does indeed look fine!

-Bruce

Reply all
Reply to author
Forward
0 new messages