Opened 12 years ago

Closed 9 years ago

#4194 closed defect (fixed)

pylab plots cut off

Reported by: malb Owned by: was
Priority: major Milestone: sage-4.7.2
Component: graphics Keywords: plot
Cc: mvgnu, mhansen, jason Merged in:
Authors: Ryan Grout Reviewers:
Report Upstream: Fixed upstream, in a later stable release. Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description

On Thursday 25 September 2008, Stan Schymanski wrote on [sage-support]:

Dear all,

When I upgraded to 3.1.2, I found that some of my plots generated using pylab in the notebooks miss their bottom bits. It seems to be related to the dpi setting. Example:

import pylab
x1 = srange(0,1.1,0.01)
d1 = [2*x+x^2 for x in x1]
pylab.clf() # clear the figure first
pylab.figure(1)
pylab.plot(x1,d1, label="d1")
pylab.ylabel("$f(x)$") # label the axes
pylab.xlabel("$x$")
pylab.savefig('foo.png',dpi=72) # fire!

If I leave the "dpi=72" out in the last line, the plot is larger and complete. This problem did not occur in sage 3.1.1, so I assume that it is a bug.

Attachments (2)

foo.png (12.0 KB) - added by kcrisman 11 years ago.
trac_4196_mpl_workaround.patch (1.3 KB) - added by ryan 10 years ago.
workaround for matplotlib 1.0.0 bug

Download all attachments as: .zip

Change History (14)

comment:1 Changed 12 years ago by mhansen

This is because matplotlib doesn't like the Sage Integer 72. If you change the 72 to int(72), then things work. We should probably write an email to the matplotlib mailing list asking them how hard it would be to make it play nicely with Sage types.

We also encounter similar issues with numpy and scipy.

Changed 11 years ago by kcrisman

comment:2 Changed 11 years ago by kcrisman

To release manager: This now works, given #5448 (and possibly earlier).

sage: import pylab
sage: x1 = srange(0,1.1,0.01)
sage: d1 = [2*x+x^2 for x in x1]
sage: pylab.clf() # clear the figure first
sage: pylab.figure(1)
<matplotlib.figure.Figure object at 0x16d41d0>
sage: pylab.plot(x1,d1, label="d1")
[<matplotlib.lines.Line2D object at 0x102ceb0>]
sage: pylab.ylabel("$f(x)$") # label the axes
<matplotlib.text.Text object at 0x4413f0>
sage: pylab.xlabel("$x$")
<matplotlib.text.Text object at 0x1038890>
sage: pylab.savefig('foo.png',dpi=72)

foo.png is attached.

comment:3 Changed 11 years ago by jason

  • Cc mvgnu added

kcrisman says this works now, so should be closed.

comment:4 Changed 11 years ago by jason

Don't close this.

Something really, really weird is going on.

In a fresh Sage session (not even in the notebook, just in a Sage console session), running the following (simplified from above) code produces png that is cut off which is about 12K:

import matplotlib.pyplot as plt
import numpy
plt.figure()
plt.plot(numpy.arange(0,1.1,0.01))
plt.savefig('foo.png',dpi=72) # fire!

However, immediately saving the figure again using plt.savefig('foo.png',dpi=72) writes a 13K file which is not cut off.

Doing the same test with sage -python yields the correct figure the first time. Doing the same test with the system python yields the correct figure the first time. This is with the matplotlib 0.99.1 spkg installed.

comment:5 Changed 11 years ago by jason

  • Cc mhansen added

comment:6 Changed 11 years ago by jason

I've posted to the matplotlib-user mailing list today about this issue.

comment:7 Changed 10 years ago by ryan

  • Report Upstream set to N/A

With sage 4.6.0 it is still cut off (matplotlib 1.0.0)

Changed 10 years ago by ryan

workaround for matplotlib 1.0.0 bug

comment:9 Changed 10 years ago by ryan

  • Authors set to Ryan Grout
  • Status changed from new to needs_review

according to the matplotlib-users discussion, the issue arises because the file is not flushed before being closed. This patch correctly opens, writes and most importantly flush the file before closing.

The bug still appears to exist upstream.

comment:10 Changed 10 years ago by ryan

  • Cc jason added
  • Report Upstream changed from N/A to Fixed upstream, in a later stable release.
  • Status changed from needs_review to needs_work

comment:11 Changed 10 years ago by ryan

see #10588. Upgrading to matplotlib 1.0.1 should fix the issue

comment:12 Changed 9 years ago by jason

  • Resolution set to fixed
  • Status changed from needs_work to closed

Indeed, this is now fixed.

Note: See TracTickets for help on using tickets.