[Tutor] New plot over the old graph

Andreas Perstinger andreas.perstinger at gmx.net
Thu Dec 1 18:25:30 CET 2011


On 2011-12-01 14:30, stm atoc wrote:
> With your help, I have a good script from the previous discussion:
>
> **********
> from pylab import *

Have you used MATLAB before and are used to its syntax? In general "star 
imports" (from xxx import *) are a bad practice and IMHO should be avoided.

> import numpy
> import matplotlib.pyplot as pyplot
> import matplotlib.mlab as mlab

These imports are unnecessary if you use the first line because "pylab" 
imports everything from "numpy" and "matplotlib" into a single 
namespace. So either use just the first line (not recommended) or the 
following line (recommended).

See also
http://matplotlib.sourceforge.net/faq/usage_faq.html#matplotlib-pylab-and-pyplot-how-are-they-related
and
http://matplotlib.sourceforge.net/faq/usage_faq.html#coding-styles

BTW: Why do you import "mlab" when you don't use it?

> with open("ourtest_out.list", "r") as f:
>     z = numpy.array([float(v) for v in f.readline().split()[1:]])
>
> a = numpy.loadtxt("ourtest_out.list", skiprows=3)
> N = 100
> Conc = a[0:, N+1:]
> print len(Conc[0]) == len(z)

This line was just for testing. You can delete it without any consequences.


> figure()
>
> pyplot.plot(Conc[0],z,'r-',label='initial')
> pyplot.plot(Conc[1],z,'b-',label='after 20s')
>
> show()

Isn't that what you want? You are plotting all your data in one graph. 
There is a straight red line on the left side and a falling blue line 
from left to right.

> *********
>
> I have tried to make subplot for this case as follows:
>
> pyplot.subplot(111)
> pyplot.plot(Conc[0],z,'r-',label='initial')
> pyplot.plot(Conc[1],z,'b-',label='after 20s')

Here you are creating a subplot with 1 plot each row and 1 plot each 
column, in other words you do the same as above (creating just 1 plot). 
If you want to have for example 4 plots in the same window with 2 each 
row and 2 each column you have to use

pyplot.subplot(221)

After plotting all the data in this first "axes" you have to switch to 
the next one:

pyplot.subplot(222)

Have you already read the matplotlib-tutorial:
http://matplotlib.sourceforge.net/users/pyplot_tutorial.html

> However, I am not sure how to add new data over this to make a graph
> including both new and old data simultaneously.

As I've said before: You are already plotting all data in one graph.
Don't you get two different lines?

Bye, Andreas


More information about the Tutor mailing list