@@ -1448,7 +1448,8 @@ def polar(*args, **kwargs):
1448
1448
return ret
1449
1449
1450
1450
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 ,
1452
1453
** kwargs ):
1453
1454
"""
1454
1455
Plot the data in *fname*
@@ -1464,17 +1465,27 @@ def plotfile(fname, cols=(0,), plotfuncs=None,
1464
1465
1465
1466
- If len(*cols*) > 1, the first element will be an identifier for
1466
1467
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*.
1468
1471
1469
1472
*plotfuncs*, if not *None*, is a dictionary mapping identifier to
1470
1473
an :class:`~matplotlib.axes.Axes` plotting function as a string.
1471
1474
Default is 'plot', other choices are 'semilogy', 'fill', 'bar',
1472
1475
etc. You must use the same type of identifier in the *cols*
1473
1476
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.
1475
1481
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.
1478
1489
1479
1490
kwargs are passed on to plotting functions.
1480
1491
@@ -1484,17 +1495,26 @@ def plotfile(fname, cols=(0,), plotfuncs=None,
1484
1495
plotfile(fname, (0,1,3))
1485
1496
1486
1497
# 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.
1488
1504
"""
1489
1505
1490
- fig = figure ()
1506
+ if newfig :
1507
+ fig = figure ()
1508
+ else :
1509
+ fig = gcf ()
1510
+
1491
1511
if len (cols )< 1 :
1492
1512
raise ValueError ('must have at least one column of data' )
1493
1513
1494
1514
if plotfuncs is None :
1495
1515
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 )
1498
1518
1499
1519
def getname_val (identifier ):
1500
1520
'return the name and column data for identifier'
@@ -1507,36 +1527,44 @@ def getname_val(identifier):
1507
1527
raise TypeError ('identifier must be a string or integer' )
1508
1528
1509
1529
xname , x = getname_val (cols [0 ])
1530
+ ynamelist = []
1510
1531
1511
1532
if len (cols )== 1 :
1512
1533
ax1 = fig .add_subplot (1 ,1 ,1 )
1513
1534
funcname = plotfuncs .get (cols [0 ], 'plot' )
1514
1535
func = getattr (ax1 , funcname )
1515
1536
func (x , ** kwargs )
1516
- ax1 .set_xlabel (xname )
1537
+ ax1 .set_ylabel (xname )
1517
1538
else :
1518
1539
N = len (cols )
1519
1540
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 )
1526
1550
1527
1551
1528
1552
yname , y = getname_val (cols [i ])
1553
+ ynamelist .append (yname )
1529
1554
1530
1555
funcname = plotfuncs .get (cols [i ], 'plot' )
1531
1556
func = getattr (ax , funcname )
1532
1557
1533
1558
func (x , y , ** kwargs )
1534
- ax .set_ylabel (yname )
1559
+ if subplots :
1560
+ ax .set_ylabel (yname )
1535
1561
if ax .is_last_row ():
1536
1562
ax .set_xlabel (xname )
1537
1563
else :
1538
1564
ax .set_xlabel ('' )
1539
1565
1566
+ if not subplots :
1567
+ ax .legend (ynamelist , loc = 'best' )
1540
1568
1541
1569
if xname == 'date' :
1542
1570
fig .autofmt_xdate ()
0 commit comments