Hello all,
first of all I want to thank the developers for the plotfile-function.
Nevertheless I would like to report a bug of the function and propose addional
functionality. I added a patch including the following changes:
- circumvent the NameError in the case of len(cols)==1 (N is not defined)
- using fig = gcf() instead of fig = figure() to supress opening a new figure
and therewith allowing to use the user preferred figure
- added keyword argument 'names' to set x/ylabels in the case there are no
names in the csv-file
- changed the plotfile_demo.py accordingly
I would be happy, if this is helpful to others too.
regards Matthias
Index: lib/matplotlib/pyplot.py
===================================================================
--- lib/matplotlib/pyplot.py (revision 5129)
+++ lib/matplotlib/pyplot.py (working copy)
@@ -1221,7 +1221,7 @@
return ret
def plotfile(fname, cols=(0,), plotfuncs=None,
- comments='#', skiprows=0, checkrows=5, delimiter=',',
+ comments='#', skiprows=0, checkrows=5, delimiter=',', names=None,
**kwargs):
"""
plot the data in fname
@@ -1245,7 +1245,7 @@
plotfuncs dictionary, eg integer column numbers in both or column
names in both.
- comments, skiprows, checkrows, and delimiter are all passed on to
+ comments, skiprows, checkrows, delimiter and names are all passed on to
matplotlib.mlab.csv2rec to load the data into a record array. See
the help there fore more information.
@@ -1257,17 +1257,18 @@
plotfile(fname, (0,1,3))
# plot using column names; specify an alternate plot type for volume
- plotfile(fname, ('date', 'volume', 'adj_close'), plotfuncs={'volume': 'semilogy'})
+ plotfile(fname, ('date', 'volume', 'adj_close'),
+ plotfuncs={'volume': 'semilogy'})
"""
- fig = figure()
+ fig = gcf()
if len(cols)<1:
raise ValueError('must have at least one column of data')
if plotfuncs is None:
plotfuncs = dict()
- r = mlab.csv2rec(fname, comments=comments,
- skiprows=skiprows, checkrows=checkrows, delimiter=delimiter)
+ r = mlab.csv2rec(fname, comments=comments, skiprows=skiprows,
+ checkrows=checkrows, delimiter=delimiter, names=names)
def getname_val(identifier):
'return the name and column data for identifier'
@@ -1282,7 +1283,7 @@
xname, x = getname_val(cols[0])
if len(cols)==1:
- ax1 = fig.add_subplot(N,1,i)
+ ax1 = gca()
funcname = plotfuncs.get(cols[0], 'plot')
func = getattr(ax1, funcname)
func(x, **kwargs)
@@ -1292,11 +1293,10 @@
for i in range(1,N):
if i==1:
ax = ax1 = fig.add_subplot(N-1,1,i)
- ax.grid(True)
else:
ax = fig.add_subplot(N-1,1,i, sharex=ax1)
- ax.grid(True)
+ ax.grid(True)
yname, y = getname_val(cols[i])
Index: examples/plotfile_demo.py
===================================================================
--- examples/plotfile_demo.py (revision 5129)
+++ examples/plotfile_demo.py (working copy)
@@ -1,22 +1,34 @@
-from pylab import plotfile, show
+from pylab import figure, plotfile, show
fname = 'data/msft.csv'
+fname2 = 'data/some_data.csv'
# test 1; use ints
+figure(1)
plotfile(fname, (0,5,6))
# test 2; use names
+figure(2)
plotfile(fname, ('date', 'volume', 'adj_close'))
# test 3; use semilogy for volume
-plotfile(fname, ('date', 'volume', 'adj_close'), plotfuncs={'volume': 'semilogy'})
+figure(3)
+plotfile(fname, ('date', 'volume', 'adj_close'),
+ plotfuncs={'volume': 'semilogy'})
# test 4; use semilogy for volume
+figure(4)
plotfile(fname, (0,5,6), plotfuncs={5:'semilogy'})
# test 5; use bar for volume
+figure(5)
plotfile(fname, (0,5,6), plotfuncs={5:'bar'})
+# test 6; labeling, if no names in csv-file
+figure(6)
+plotfile(fname2, cols=(0,1,2), delimiter=' ',
+ names=['$x$', '$f(x)=x^2$', '$f(x)=x^3$'])
+
show()
Index: examples/data/some_data.csv
===================================================================
--- examples/data/some_data.csv (revision 0)
+++ examples/data/some_data.csv (revision 0)
@@ -0,0 +1,11 @@
+ 0 0 0
+ 1 1 1
+ 2 4 8
+ 3 9 27
+ 4 16 64
+ 5 25 125
+ 6 36 216
+ 7 49 343
+ 8 64 512
+ 9 81 729
+10 100 1000
Property changes on: examples/data/some_data.csv
___________________________________________________________________
Name: svn:executable
+ *
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users