Skip to content

Commit bdc188f

Browse files
committed
Added options to plotfile
svn path=/trunk/matplotlib/; revision=7078
1 parent 53b2575 commit bdc188f

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
======================================================================
2+
2009-05-02 Added options to plotfile based on question from
3+
Joseph Smidt and patch by Matthias Michler. - EF
4+
25
2009-05-01 Changed add_artist and similar Axes methods to
36
return their argument. - EF
47

examples/data/data_x_x2_x3.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
0 0 0
2+
1 1 1
3+
2 4 8
4+
3 9 27
5+
4 16 64
6+
5 25 125
7+
6 36 216
8+
7 49 343
9+
8 64 512
10+
9 81 729
11+
10 100 1000

examples/pylab_examples/plotfile_demo.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from pylab import plotfile, show
1+
from pylab import plotfile, show, gca
22

33
fname = '../data/msft.csv'
4+
fname2 = '../data/data_x_x2_x3.csv'
45

56
# test 1; use ints
67
plotfile(fname, (0,5,6))
@@ -14,7 +15,20 @@
1415
# test 4; use semilogy for volume
1516
plotfile(fname, (0,5,6), plotfuncs={5:'semilogy'})
1617

17-
# test 5; use bar for volume
18+
#test 5; single subplot
19+
plotfile(fname, ('date', 'open', 'high', 'low', 'close'), subplots=False)
20+
21+
# test 6; labeling, if no names in csv-file
22+
plotfile(fname2, cols=(0,1,2), delimiter=' ',
23+
names=['$x$', '$f(x)=x^2$', '$f(x)=x^3$'])
24+
25+
# test 7; more than one file per figure--illustrated here with a single file
26+
plotfile(fname2, cols=(0, 1), delimiter=' ')
27+
plotfile(fname2, cols=(0, 2), newfig=False, delimiter=' ') # use current figure
28+
gca().set_xlabel(r'$x$')
29+
gca().set_ylabel(r'$f(x) = x^2, x^3$')
30+
31+
# test 8; use bar for volume
1832
plotfile(fname, (0,5,6), plotfuncs={5:'bar'})
1933

2034
show()

lib/matplotlib/pyplot.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,8 @@ def polar(*args, **kwargs):
14481448
return ret
14491449

14501450
def plotfile(fname, cols=(0,), plotfuncs=None,
1451-
comments='#', skiprows=0, checkrows=5, delimiter=',',
1451+
comments='#', skiprows=0, checkrows=5, delimiter=',', names=None,
1452+
subplots=True, newfig=True,
14521453
**kwargs):
14531454
"""
14541455
Plot the data in *fname*
@@ -1464,17 +1465,27 @@ def plotfile(fname, cols=(0,), plotfuncs=None,
14641465
14651466
- If len(*cols*) > 1, the first element will be an identifier for
14661467
data for the *x* axis and the remaining elements will be the
1467-
column indexes for multiple subplots
1468+
column indexes for multiple subplots if *subplots* is *True*
1469+
(the default), or for lines in a single subplot if *subplots*
1470+
is *False*.
14681471
14691472
*plotfuncs*, if not *None*, is a dictionary mapping identifier to
14701473
an :class:`~matplotlib.axes.Axes` plotting function as a string.
14711474
Default is 'plot', other choices are 'semilogy', 'fill', 'bar',
14721475
etc. You must use the same type of identifier in the *cols*
14731476
vector as you use in the *plotfuncs* dictionary, eg., integer
1474-
column numbers in both or column names in both.
1477+
column numbers in both or column names in both. If *subplots*
1478+
is *False*, then including any function such as 'semilogy'
1479+
that changes the axis scaling will set the scaling for all
1480+
columns.
14751481
1476-
*comments*, *skiprows*, *checkrows*, and *delimiter* are all passed on to
1477-
:func:`matplotlib.pylab.csv2rec` to load the data into a record array.
1482+
*comments*, *skiprows*, *checkrows*, *delimiter*, and *names*
1483+
are all passed on to :func:`matplotlib.pylab.csv2rec` to
1484+
load the data into a record array.
1485+
1486+
If *newfig* is *True*, the plot always will be made in a new figure;
1487+
if *False*, it will be made in the current figure if one exists,
1488+
else in a new figure.
14781489
14791490
kwargs are passed on to plotting functions.
14801491
@@ -1484,17 +1495,26 @@ def plotfile(fname, cols=(0,), plotfuncs=None,
14841495
plotfile(fname, (0,1,3))
14851496
14861497
# plot using column names; specify an alternate plot type for volume
1487-
plotfile(fname, ('date', 'volume', 'adj_close'), plotfuncs={'volume': 'semilogy'})
1498+
plotfile(fname, ('date', 'volume', 'adj_close'),
1499+
plotfuncs={'volume': 'semilogy'})
1500+
1501+
Note: plotfile is intended as a convenience for quickly plotting
1502+
data from flat files; it is not intended as an alternative
1503+
interface to general plotting with pyplot or matplotlib.
14881504
"""
14891505

1490-
fig = figure()
1506+
if newfig:
1507+
fig = figure()
1508+
else:
1509+
fig = gcf()
1510+
14911511
if len(cols)<1:
14921512
raise ValueError('must have at least one column of data')
14931513

14941514
if plotfuncs is None:
14951515
plotfuncs = dict()
1496-
r = mlab.csv2rec(fname, comments=comments,
1497-
skiprows=skiprows, checkrows=checkrows, delimiter=delimiter)
1516+
r = mlab.csv2rec(fname, comments=comments, skiprows=skiprows,
1517+
checkrows=checkrows, delimiter=delimiter, names=names)
14981518

14991519
def getname_val(identifier):
15001520
'return the name and column data for identifier'
@@ -1507,36 +1527,44 @@ def getname_val(identifier):
15071527
raise TypeError('identifier must be a string or integer')
15081528

15091529
xname, x = getname_val(cols[0])
1530+
ynamelist = []
15101531

15111532
if len(cols)==1:
15121533
ax1 = fig.add_subplot(1,1,1)
15131534
funcname = plotfuncs.get(cols[0], 'plot')
15141535
func = getattr(ax1, funcname)
15151536
func(x, **kwargs)
1516-
ax1.set_xlabel(xname)
1537+
ax1.set_ylabel(xname)
15171538
else:
15181539
N = len(cols)
15191540
for i in range(1,N):
1520-
if i==1:
1521-
ax = ax1 = fig.add_subplot(N-1,1,i)
1522-
ax.grid(True)
1523-
else:
1524-
ax = fig.add_subplot(N-1,1,i, sharex=ax1)
1525-
ax.grid(True)
1541+
if subplots:
1542+
if i==1:
1543+
ax = ax1 = fig.add_subplot(N-1,1,i)
1544+
else:
1545+
ax = fig.add_subplot(N-1,1,i, sharex=ax1)
1546+
elif i==1:
1547+
ax = fig.add_subplot(1,1,1)
1548+
1549+
ax.grid(True)
15261550

15271551

15281552
yname, y = getname_val(cols[i])
1553+
ynamelist.append(yname)
15291554

15301555
funcname = plotfuncs.get(cols[i], 'plot')
15311556
func = getattr(ax, funcname)
15321557

15331558
func(x, y, **kwargs)
1534-
ax.set_ylabel(yname)
1559+
if subplots:
1560+
ax.set_ylabel(yname)
15351561
if ax.is_last_row():
15361562
ax.set_xlabel(xname)
15371563
else:
15381564
ax.set_xlabel('')
15391565

1566+
if not subplots:
1567+
ax.legend(ynamelist, loc='best')
15401568

15411569
if xname=='date':
15421570
fig.autofmt_xdate()

0 commit comments

Comments
 (0)