Another Gnuplot style question - matplotlib-users - Matplotlib
Another Gnuplot style question Community matplotlib-users Joseph_Smidt April 29, 2009, 6:20am 1 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. 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 ··· -- ------------------------------------------------------------------------ Joseph Smidt < [email protected] ...> Physics and Astronomy 4129 Frederick Reines Hall Irvine, CA 92697-4575 Office: 949-824-3269 _Sandro_Tosi2 April 29, 2009, 7:15am 2 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. a simple example: In [2]: import numpy as np In [3]: import matplotlib.pyplot as plt In [16]: data = np.random.rand(10,3) In [17]: data Out[17]: array([[ 0.00669083, 0.4421283 , 0.46697081], [ 0.18093819, 0.11669917, 0.70887601], [ 0.11659791, 0.96514955, 0.07389404], [ 0.95616662, 0.30350482, 0.10036185], [ 0.14197553, 0.10560376, 0.2964961 ], [ 0.74705585, 0.21806946, 0.37095176], [ 0.1551145 , 0.76093425, 0.878701 ], [ 0.44315466, 0.3625146 , 0.06750168], [ 0.96109656, 0.88401174, 0.59215722], [ 0.46190334, 0.39079641, 0.5958516 ]]) In [28]: plt.semilogy(data[:,0], data[:,1], label='first dataset') Out[28]: [<matplotlib.lines.Line2D object at 0xa7698ac>] In [29]: plt.semilogy(data[:,0], data[:,2], label='second dataset') Out[29]: [<matplotlib.lines.Line2D object at 0xa8049ac>] In [30]: plt.xlim([0.2, 0.8]) # limit x to 0.2..0.8 Out[30]: (0.20000000000000001, 0.80000000000000004) In [31]: plt.legend() Out[31]: <matplotlib.legend.Legend object at 0xa80d04c> In [32]: plt.show() 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. to load data from file you can try matplotlib.mlab.csv2rec Cheers, ··· On Wed, Apr 29, 2009 at 08:20, Joseph Smidt < [email protected] ...> wrote: -- Sandro Tosi (aka morph, morpheus, matrixhasu) My website: http://matrixhasu.altervista.org/ Me at Debian: http://wiki.debian.org/SandroTosi Eric_Firing1 April 29, 2009, 7:20am 3 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 Joseph_Smidt April 29, 2009, 8:09am 4 Thanks everyone, this is exactly what I wanted. ··· -- ------------------------------------------------------------------------ Joseph Smidt < [email protected] ...> Physics and Astronomy 4129 Frederick Reines Hall Irvine, CA 92697-4575 Office: 949-824-3269 Matthias_Michler April 29, 2009, 8:31am 5 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 plotfile_extended.patch (2.1 KB) plotfile_demo.py (902 Bytes) data_x_x2_x3.csv (132 Bytes) ··· 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
The rest of this page is on the live site.