[Tutor] New plot over the old graph

Andreas Perstinger andreas.perstinger at gmx.net
Thu Dec 1 22:01:06 CET 2011


On 2011-12-01 19:20, stm atoc wrote:
> Thanks for all information/websites and advice. Yes the graph is
> exactly like the one you mentioned. Also, I would like to have them in
> one not two, but I think since the dimension of the x and y are not
> same, I have no choice.
>
>   What I like to do now is comparing 2 (later 3 or more) different sets
> of data, e.g. comparison among Conc[1] with sets....
>
> I have changed the script like this:
>
> with open("ourtest_out.list", "r") as f:
>     z = numpy.array([float(v) for v in f.readline().split()[1:]])
>
> a1 = numpy.loadtxt("ourtest_out1.list", skiprows=3)
> a2 = numpy.loadtxt("ourtest_out2.list", skiprows=3)
> a3 = numpy.loadtxt("ourtest_out3.list", skiprows=3)
>
> N = 100
>
> Conc1 = a1[0:, N+1:] #base case
> Conc2 = a2[0:, N+1:] # Ydw=0.1
> Conc3 = a3[0:, N+1:] # nuh=0.01
> lw = 2.0 #linewidth

You aren't using "lw" so it doesn't make sense to define it.

> dpi = 96
> figure(figsize=(10,6),dpi=dpi)

I prefer to not clutter up the namespace with "star imports" (from 
pylabs import *) but it's your choice.

>
> pyplot.subplot(111)

If you just use one graph/figure this call is unnecessary.

> pyplot.plot(Conc1[1], z)
> pyplot.plot(Conc2[1], z)
> pyplot.plot(Conc3[1], z)
> pyplot.xlim(0,1)
>
> plt.xlabel('Conc')
> plt.ylabel('z')

I assume you've got these lines from the tutorial. But there they are 
using the following import:

import matplotlib.pyplot as plt

I've used

import matplotlib.pyplot as pyplot

so you have to decide which name you want to use (You can't mix both).

In general, if you just use

import matplotlib.pyplot

you would have to use always the full name:

matplotlib.pyplot.xlabel('Conc')

But with the "as"-keyword you can choose, which name gets imported into 
the namespace.

If you have problems understanding imports and namespaces look at Alan's 
tutorial:
http://www.freenetpages.co.uk/hp/alan.gauld/tutfunc.htm (section "Using 
Modules")
http://www.freenetpages.co.uk/hp/alan.gauld/tutname.htm (about Namespaces)

>
> pyplot.grid(True)
> show()
> savefig('Conc.png')

You should call "savefig" before "show" because in non-interactive mode 
(calling the script from the commandline) "show" will block all figures 
until they are closed. So after "show" there won't be any figures left 
and "savefig" will write an empty figure to the file.

> close()
>
> This can give me the comparison in one graph, I suppose.
> Now, first I like to know if this is a fine/logical script. otherwise
> I would like to know about probably a better way to write it with less
> lines!

You could write the whole script in a more object-oriented style where 
you create a figure-instance and then set the attributes you want 
instead of calling all the functions. But for the beginning it's ok.

> and second, when I do plot, each grid between x or y axis, has a
> thickness of 0.2. what I like do is to change it to 0.1 grid . So, I
> couldn't find it through matplotlib website (at least with my
> searching. Would it be possible helping me about?

You set the scale with the "xticks"-function (or the corresponding 
"yticks"):
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.xticks

So in your case you could use

pyplot.xticks(numpy.arange(0, 1.1, 0.1))

Bye, Andreas


More information about the Tutor mailing list