@@ -1448,7 +1448,8 @@ def polar(*args, **kwargs):
14481448 return ret
14491449
14501450def 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