213
213
from artist import getp , get
214
214
from artist import setp as _setp
215
215
216
+
217
+
218
+ import matplotlib .cbook as cbook
219
+ from matplotlib .mlab import csv2rec
220
+
216
221
# a hack to keep old versions of ipython working with mpl after bug
217
222
# fix #1209354
218
223
if 'IPython.Shell' in sys .modules :
@@ -1500,6 +1505,103 @@ def box(on=None):
1500
1505
ax .set_frame_on (on )
1501
1506
draw_if_interactive ()
1502
1507
1508
+
1509
+
1510
+
1511
+ def plotfile (fname , cols = (0 ,), plotfuncs = None ,
1512
+ comments = '#' , skiprows = 0 , checkrows = 5 , delimiter = ',' ,
1513
+ ** kwargs ):
1514
+ """
1515
+ plot the data in fname
1516
+
1517
+ cols is a sequence of column identifiers to plot. An identifier
1518
+ is either an int or a string. if it is an int, it indicates the
1519
+ column number. If it is a string, it indicates the column header.
1520
+ mpl will make column headers lower case, replace spaces with
1521
+ strings, and remove all illegal characters; so 'Adj Close*' will
1522
+ have name 'adj_close'
1523
+
1524
+ if len(cols)==1, only that column will be plotted on the y axis.
1525
+ if len(cols)>1, the first element will be an identifier for data
1526
+ for the x axis and the remaining elements will be the column
1527
+ indexes for multiple subplots
1528
+
1529
+ plotfuncs, if not None, is a dictionary mapping identifier to an
1530
+ Axes plotting function as a string. Default is 'plot', other
1531
+ choices are 'semilogy', 'fill', 'bar', etc... You must use the
1532
+ same type of identifier in the cols vector as you use in the
1533
+ plotfuncs dictionary, eg integer column numbers in both or column
1534
+ names in both.
1535
+
1536
+ comments, skiprows, checkrows, and delimiter are all passed on to
1537
+ matplotlib.mlab.csv2rec to load the data into a record array
1538
+
1539
+ kwargs are passed on to plotting functions
1540
+
1541
+ Example usage:
1542
+
1543
+ # plot the 2nd and 4th column against the 1st in two subplots
1544
+ plotfile(fname, (0,1,3))
1545
+
1546
+ # plot using column names; specify an alternate plot type for volume
1547
+ plotfile(fname, ('date', 'volume', 'adj_close'), plotfuncs={'volume': 'semilogy'})
1548
+ """
1549
+
1550
+ fig = figure ()
1551
+ if len (cols )< 1 :
1552
+ raise ValueError ('must have at least one column of data' )
1553
+
1554
+ if plotfuncs is None :
1555
+ plotfuncs = dict ()
1556
+ r = csv2rec (fname , comments = comments ,
1557
+ skiprows = skiprows , checkrows = checkrows , delimiter = delimiter )
1558
+
1559
+ def getname_val (identifier ):
1560
+ 'return the name and column data for identifier'
1561
+ if cbook .is_string_like (identifier ):
1562
+ return identifier , r [identifier ]
1563
+ elif cbook .is_numlike (identifier ):
1564
+ name = r .dtype .names [int (identifier )]
1565
+ return name , r [name ]
1566
+
1567
+
1568
+ xname , x = getname_val (cols [0 ])
1569
+
1570
+ if len (cols )== 1 :
1571
+ ax1 = fig .add_subplot (N ,1 ,i )
1572
+ funcname = plotfuncs .get (cols [0 ], 'plot' )
1573
+ func = getattr (ax1 , funcname )
1574
+ func (x , ** kwargs )
1575
+ ax1 .set_xlabel (xname )
1576
+ else :
1577
+ N = len (cols )
1578
+ for i in range (1 ,N ):
1579
+ if i == 1 :
1580
+ ax = ax1 = fig .add_subplot (N - 1 ,1 ,i )
1581
+ ax .grid (True )
1582
+ else :
1583
+ ax = fig .add_subplot (N - 1 ,1 ,i , sharex = ax1 )
1584
+ ax .grid (True )
1585
+
1586
+
1587
+ yname , y = getname_val (cols [i ])
1588
+
1589
+ funcname = plotfuncs .get (cols [i ], 'plot' )
1590
+ func = getattr (ax , funcname )
1591
+
1592
+ func (x , y , ** kwargs )
1593
+ ax .set_ylabel (yname )
1594
+ if ax .is_last_row ():
1595
+ ax .set_xlabel (xname )
1596
+ else :
1597
+ ax .set_xlabel ('' )
1598
+
1599
+
1600
+ if xname == 'date' :
1601
+ fig .autofmt_xdate ()
1602
+
1603
+
1604
+
1503
1605
### Deprecated functions:
1504
1606
def scatter_classic (* args , ** kwargs ):
1505
1607
return gca ().scatter_classic (* args , ** kwargs )
@@ -1511,6 +1613,8 @@ def pcolor_classic(*args, **kwargs):
1511
1613
if Axes .pcolor_classic .__doc__ is not None :
1512
1614
pcolor_classic .__doc__ = dedent (Axes .pcolor_classic .__doc__ )
1513
1615
1616
+
1617
+
1514
1618
### Do not edit below this point
1515
1619
# This function was autogenerated by boilerplate.py. Do not edit as
1516
1620
# changes will be lost
0 commit comments