Hello Eric, Hello list,

a year ago I also encountered the problem of "one file - one figure" of the 
plotfile function. I would like to propose an addional functionality of using 
one figure and several files in plotfile, because sometimes I don't want to 
read data myself. I added a patch including the following changes:
- added a new keywordargument to plotfile 'use_cf': If use_cf isTrue plotfile 
uses fig = gcf() instead of fig = figure() to suppress opening of a new 
figure and therewith allowing to use the user preferred figure
- added a further new keyword argument 'names' to set x/ylabels in the case 
there are no names in the csv-file

Furthermore I attached the modified plotfile_demo.py 
(examples/pylab_examples/plotfile_demo.py) and some new data 
(examples/data/data_x_x2_x3.csv).

Could this be useful?

Thanks in advance for any comments.

best regards
Matthias

On Wednesday 29 April 2009 09:20:17 Eric Firing wrote:
> Joseph Smidt wrote:
> > Okay, I am another gnuplot user trying to migrate over to matplotlib.
> > I like what I see, but there are a couple things that are very easy to
> > do in Gnuplot that I can't figure out how to do with matplotlib.
> >
> > I have a file with 3 columns of data called data.txt that looks like:
> >
> > 0.0000      1.0000     1.0
> > 0.0634      1.0655      1.1353
> > 0.1269      1.1353      1.28899916094
> > 0.1903      1.2097      1.46345358199
> > 0.2538      1.2889     1.6615188369
> > 0.3173      1.3734     1.88639043926
> > ...
> >
> > I can plot this data, 2 versus 1 and 3 versus 1, very easily on the
> > same plot, with a legend, with log y values, and only for the xrange
> > between 2 and 3 with gnuplot:
> >
> > set log y
> > set xrange[2:3]
> > plot 'data.txt' u 1:2 w l t 'apples', 'data.txt' u 1:3 w l t 'oranges'
> >
> > Now, how do I do that same thing with matplotlob?  Ie:
> >
> > 1. Both graphs overlayed on the same plot.
> > 2. Semilogy. (log y values),
> > 3. Only ploy for x in the range 2-3.
> > 4. Legend for the two graphs on same plot.
>
> Something like this:
>
> import numpy as np
> import matplotlib.pyplot as plt
>
> x, apples, oranges = np.loadtxt('data.txt', unpack=True)
> plt.semilogy(x, apples, label='apples')
> plt.semilogy(x, oranges, label='oranges')
> plt.legend()
> plt.gca().set_xlim(2, 3)
> plt.show()
>
> There are many possible variations and styles.  The basic point is to
> separate reading in the data from plotting it.  Plotfile won't do what
> you want because it is designed to make separate subplots instead of
> plotting multiple lines on a single axes.  Maybe doing the latter would
> be at least as useful, if not more, and could be enabled as an option
> with one more kwarg.
>
> Eric
>
> > I have spent time looking through the documentation but I can't find
> > anyway to do this is any straightforward way.  plotfile() looks
> > promising, but I can't seem to make it do the above.  Thanks in
> > advance.
> >
> >                              Joseph Smidt
>
> ---------------------------------------------------------------------------
>--- Register Now & Save for Velocity, the Web Performance & Operations
> Conference from O'Reilly Media. Velocity features a full day of
> expert-led, hands-on workshops and two days of sessions from industry
> leaders in dedicated Performance & Operations tracks. Use code vel09scf
> and Save an extra 15% before 5/3. http://p.sf.net/sfu/velocityconf
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Index: lib/matplotlib/pyplot.py
===================================================================
--- lib/matplotlib/pyplot.py	(revision 7068)
+++ lib/matplotlib/pyplot.py	(working copy)
@@ -1447,8 +1447,8 @@
     draw_if_interactive()
     return ret
 
-def plotfile(fname, cols=(0,), plotfuncs=None,
-             comments='#', skiprows=0, checkrows=5, delimiter=',',
+def plotfile(fname, cols=(0,), plotfuncs=None, use_cf=False,
+             comments='#', skiprows=0, checkrows=5, delimiter=',', names=None,
              **kwargs):
     """
     Plot the data in *fname*
@@ -1473,9 +1473,12 @@
     vector as you use in the *plotfuncs* dictionary, eg., integer
     column numbers in both or column names in both.
 
-    *comments*, *skiprows*, *checkrows*, and *delimiter* are all passed on to
-    :func:`matplotlib.pylab.csv2rec` to load the data into a record array.
+    *use_cf* : use current figure instead of a new figure for plotting
 
+    *comments*, *skiprows*, *checkrows*, *delimiter*, and *names* are all
+    passed on to :func:`matplotlib.pylab.csv2rec` to load the data into a
+    record array. 
+
     kwargs are passed on to plotting functions.
 
     Example usage::
@@ -1484,17 +1487,21 @@
       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'})
     """
+    if use_cf is False:
+        fig = figure()
+    else:
+        fig = gcf()
 
-    fig = figure()
     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'

Attachment: plotfile_demo.py
Description: application/python

 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
------------------------------------------------------------------------------
Register Now & Save for Velocity, the Web Performance & Operations 
Conference from O'Reilly Media. Velocity features a full day of 
expert-led, hands-on workshops and two days of sessions from industry 
leaders in dedicated Performance & Operations tracks. Use code vel09scf 
and Save an extra 15% before 5/3. http://p.sf.net/sfu/velocityconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to