Skip to content

Commit 926b070

Browse files
committed
added save to file object in agg
svn path=/trunk/matplotlib/; revision=544
1 parent af03698 commit 926b070

File tree

7 files changed

+86
-32
lines changed

7 files changed

+86
-32
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
New entries should be added at the top
22

33
==============================================================
4+
2004-09-28 Added save to file object for agg - see
5+
examples/print_stdout.py
46

57
2004-09-24 Reorganized all py code to lib subdir
68

examples/fill_between.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from matplotlib.matlab import *
2+
from matplotlib.patches import Polygon
3+
4+
def fill_between(ax, x, y1, y2, **kwargs):
5+
# add x,y2 in reverse order
6+
verts = zip(x,y1) + [(x[i], y2[i]) for i in range(len(x)-1,-1,-1)]
7+
poly = Polygon(verts, **kwargs)
8+
ax.add_patch(poly)
9+
ax.autoscale_view()
10+
return poly
11+
12+
x = arange(0, 2, 0.01)
13+
y1 = sin(2*pi*x)
14+
y2 = sin(4*pi*x) + 2
15+
ax = gca()
16+
17+
p = fill_between(ax, x, y1, y2, facecolor='g')
18+
p.set_alpha(0.5)
19+
show()
20+

examples/print_stdout.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# print png to standard out
2+
# usage: python print_stdout.py > somefile.png
3+
import sys
4+
import matplotlib
5+
matplotlib.use('Agg')
6+
from matplotlib.matlab import *
7+
8+
plot([1,2,3])
9+
10+
savefig(sys.stdout)
11+
show()

examples/simple_plot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
figure(1)
66
t = arange(0.0, 1.0, 0.01)
77
s = cos(2*2*pi*t)
8-
plot(t, s, antialiased=False)
8+
plot(t, s)
99

1010
xlabel('time (s)')
1111
ylabel('voltage (mV)')
1212
title('About as simple as it gets, folks')
1313
grid(True)
1414
#axis([0,1,-1,1])
15-
savefig('simple_plot')
15+
#savefig('simple_plot')
1616

1717
show()

lib/matplotlib/backends/backend_agg.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,13 @@ def print_figure(self, filename, dpi=150,
322322
323323
If the extension matches BMP or RAW, write an RGBA bitmap file
324324
325+
If filename is a fileobject, write png to file object (thus
326+
you can, for example, write the png to stdout
325327
"""
326328
if DEBUG: print 'FigureCanvasAgg.print_figure'
327329

330+
331+
328332
# store the orig figure dpi, color and size information so we
329333
# can restore them later. For image creation alone, this is
330334
# not important since after the print the figure is done. But
@@ -344,34 +348,38 @@ def print_figure(self, filename, dpi=150,
344348
# render the printed figure
345349
self.draw()
346350

347-
# take a look at the extension and choose the print handler
348-
basename, ext = os.path.splitext(filename)
349-
if not len(ext):
350-
ext = '.png'
351-
filename += ext
352-
353-
ext = ext.lower()
354-
if (ext.find('rgb')>=0 or
355-
ext.find('raw')>=0 or
356-
ext.find('bmp')>=0 ):
357-
# agg doesn't handle unicode yet
358-
self.renderer._renderer.write_rgba(str(filename))
359-
elif ext.find('png')>=0:
360-
# agg doesn't handle unicode yet
361-
self.renderer._renderer.write_png(str(filename))
362-
#pass
363-
elif ext.find('svg')>=0:
364-
from backend_svg import FigureCanvasSVG
365-
svg = self.switch_backends(FigureCanvasSVG)
366-
svg.figure.dpi.set(72)
367-
svg.print_figure(filename, 72, facecolor, edgecolor, orientation)
368-
elif ext.find('ps')>=0 or ext.find('ep')>=0:
369-
from backend_ps import FigureCanvasPS # lazy import
370-
ps = self.switch_backends(FigureCanvasPS)
371-
ps.figure.dpi.set(72)
372-
ps.print_figure(filename, 72, facecolor, edgecolor, orientation)
351+
if isinstance(filename, file):
352+
# assume png and write to fileobject
353+
self.renderer._renderer.write_png(filename)
373354
else:
374-
error_msg('Do not know know to handle extension *%s' % ext)
355+
# take a look at the extension and choose the print handler
356+
basename, ext = os.path.splitext(filename)
357+
if not len(ext):
358+
ext = '.png'
359+
filename += ext
360+
361+
ext = ext.lower()
362+
if (ext.find('rgb')>=0 or
363+
ext.find('raw')>=0 or
364+
ext.find('bmp')>=0 ):
365+
# agg doesn't handle unicode yet
366+
self.renderer._renderer.write_rgba(str(filename))
367+
elif ext.find('png')>=0:
368+
# agg doesn't handle unicode yet
369+
self.renderer._renderer.write_png(str(filename))
370+
#pass
371+
elif ext.find('svg')>=0:
372+
from backend_svg import FigureCanvasSVG
373+
svg = self.switch_backends(FigureCanvasSVG)
374+
svg.figure.dpi.set(72)
375+
svg.print_figure(filename, 72, facecolor, edgecolor, orientation)
376+
elif ext.find('ps')>=0 or ext.find('ep')>=0:
377+
from backend_ps import FigureCanvasPS # lazy import
378+
ps = self.switch_backends(FigureCanvasPS)
379+
ps.figure.dpi.set(72)
380+
ps.print_figure(filename, 72, facecolor, edgecolor, orientation)
381+
else:
382+
error_msg('Do not know know to handle extension *%s' % ext)
375383

376384
# restore the original figure properties
377385
self.figure.dpi.set(origDPI)

lib/matplotlib/numerix.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,6 @@
6666
from na_imports import *
6767
else:
6868
raise RuntimeError("invalid numerix selector")
69+
70+
# a bug fix for blas numeric suggested by Fernando Perez
71+
matrixmultiply=dot

src/_backend_agg.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,19 @@ RendererAgg::write_png(const Py::Tuple& args)
943943

944944
args.verify_length(1);
945945

946-
std::string fileName = Py::String(args[0]);
947-
const char *file_name = fileName.c_str();
948946
FILE *fp;
947+
Py::Object o = Py::Object(args[0]);
948+
949+
if (o.isString()) {
950+
std::string fileName = Py::String(o);
951+
const char *file_name = fileName.c_str();
952+
fp = fopen(file_name, "wb");
953+
}
954+
else {
955+
if ((fp = PyFile_AsFile(o.ptr())) == NULL)
956+
throw Py::TypeError("Could not convert object to file pointer");
957+
}
958+
949959
png_structp png_ptr;
950960
png_infop info_ptr;
951961
struct png_color_8_struct sig_bit;
@@ -956,7 +966,7 @@ RendererAgg::write_png(const Py::Tuple& args)
956966
row_pointers[row] = pixBuffer + row * width * 4;
957967
}
958968

959-
fp = fopen(file_name, "wb");
969+
960970
if (fp == NULL)
961971
throw Py::RuntimeError("could not open file");
962972

0 commit comments

Comments
 (0)