[Tutor] Do loop in Python
Hi there, I am a new python user. I have a question regarding do loop. This is a simple program that I have written: - N=10 h=10.0 # [micrometer] z=-200.0 # [micrometer] num = 0.05 #m**2/s dz = 1.0 nuh=[] tmax=3600 dt=20. nu=[]height = arange(z*dz,0,dz) outfile=open('nu.dat','w') outfile.write('height, nu, nuh') for z,when in enumerate(height): for h in range(10): for N in range(10): for z in range((N-z)+(N-h)): nuh.append(0.01 * exp(-0.05*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) --- What I like to do with this program is do loop like the fortran version of as follows: do i = 2, N z(i) = z(i-1) +h(i-1) end do write(0,*) 'z ', z(1:N) write(0,*) 'when ', 'nu ','Conc ' do i= 1, N nuh(i)= 0.01d0*exp(-0.005d2*(z(i)+200)) ! turbulence diffusivity m**2/s nu(i)= num(1) + nuh(i) end do -- My problem is I am notable have the curve in the output plot as I have as a result of FORTRAN program. What happens is just having a straight line! the whole problem is in z part, which is supposed to be changed and i do not see it! So, would it be possible to take a look at it please. any suggestion would greatly appreciated. Thank you, Sue ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do loop in Python
regarding to the last email: what I am trying to do is seeing the variation of 'nu' over (changes of) 'z'. My concern is how to arrange this! Basically, I am not able to define the variation of nu by z ( 1 to 200). I am looking for a statement to show the changes of 'nu' for each step of z (as height). On the other hand, for each step, 'h' is supposed to be subtracted from 'z' (like: 200-10, 190-10...) as well, at least 10 times (which was trying to be defined as N)! I hope this is somehow clear Thanks in advance, Sue - Show quoted text - On Fri, Nov 25, 2011 at 10:16 AM, stm atoc wrote: > Hi there, > > I am a new python user. > I have a question regarding do loop. > > This is a simple program that I have written: > > - > N=10 > h=10.0 # [micrometer] > z=-200.0 # [micrometer] > num = 0.05 #m**2/s > dz = 1.0 > nuh=[] > tmax=3600 > dt=20. > nu=[]height = arange(z*dz,0,dz) > > outfile=open('nu.dat','w') > outfile.write('height, nu, nuh') > > for z,when in enumerate(height): > for h in range(10): > for N in range(10): > for z in range((N-z)+(N-h)): > > nuh.append(0.01 * exp(-0.05*(z+200.0))*dz) #turbulence > diffusivity m**2/s > nu.append(num + nuh[z]) > > --- > What I like to do with this program is do loop like the fortran > version of as follows: > > do i = 2, N > z(i) = z(i-1) +h(i-1) > > end do > > write(0,*) 'z ', z(1:N) > write(0,*) 'when ', 'nu ','Conc ' > > > do i= 1, N > > nuh(i)= 0.01d0*exp(-0.005d2*(z(i)+200)) ! turbulence diffusivity m**2/s > nu(i)= num(1) + nuh(i) > > > end do > > -- > My problem is I am notable have the curve in the output plot as I have > as a result of FORTRAN program. What happens is just having a > straight line! > the whole problem is in z part, which is supposed to be changed and i > do not see it! > > So, would it be possible to take a look at it please. any suggestion > would greatly appreciated. > > Thank you, > Sue > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do loop in Python
Thank you so much for your reply. It was very helpful information and I used it in order to improve the program Here is the new version of the program: zvalues = [-200] # starting value hvalues = [10] # starting value increments = [1, 1, 1, 1, 1, 1, 1, 1] for N in increments: h = hvalues[-1] - N hvalues.append(h) z = zvalues[-1] + h zvalues.append(z) height = arange((z)*dz,0,dz) for z,when in enumerate(height): nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) The story is like this: I should define layers and thickness and see how the diffusion profile changes over the z. height (or depth) of the total thickness or 'z'. I basically, define 'z' in 10 layers and each layer is called ' N' . Difference between each layer is 'h', which is equal 10 micrometer. Now, what I like to do is the modification of nu based on each zvalue In fact, for each 'zvalue' o'z' step, I need to calculate a different value for 'nu' based on the available equation in the program. BUT, I am not sure, exactly, how to add the new do loop of z inside another loop of nu. I have done this way as well (the other way around): height = arange((z)*dz,0,dz) for z,when in enumerate(height): for N in increments: h = hvalues[-1] - N hvalues.append(h) z = zvalues[-1] + h zvalues.append(z) nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) but still no sign of 'nu changes' over 'z'! So, would it be possible to check that again? Thanks, Sue On Fri, Nov 25, 2011 at 12:36 PM, Steven D'Aprano wrote: > stm atoc wrote: >> >> Hi there, >> >> I am a new python user. >> I have a question regarding do loop. >> >> This is a simple program that I have written: >> >> - >> N=10 >> h=10.0 # [micrometer] >> z=-200.0 # [micrometer] > > You define N, h and z here, but later on you use them as loop variables. So > these three values never get used: they are thrown away, and replaced by the > values of the loops: > > h -> 0, 1, 2, ... 9 > N -> 0, 1, 2, ... 9 > > z is especially troublesome, because it gets used for TWO loop variables, > one inside the other. The inner z loop depends on the outer z loop, which > makes it tricky to predict what values z will take. > > >> num = 0.05 #m**2/s >> dz = 1.0 >> nuh=[] >> tmax=3600 >> dt=20. >> nu=[]height = arange(z*dz,0,dz) > > What is arange? > > In physics, "height" is a scalar. But later on, you seen to use height as if > it were a collection of values. > > >> outfile=open('nu.dat','w') >> outfile.write('height, nu, nuh') >> >> for z,when in enumerate(height): >> for h in range(10): >> for N in range(10): >> for z in range((N-z)+(N-h)): >> >> nuh.append(0.01 * exp(-0.05*(z+200.0))*dz) #turbulence >> diffusivity m**2/s >> nu.append(num + nuh[z]) >> >> --- >> What I like to do with this program is do loop like the fortran >> version of as follows: >> >> do i = 2, N >> z(i) = z(i-1) +h(i-1) >> >> end do > > > How is z initialised? What is h? > > > I *think* you are trying to add a small increment to each value, based on > the previous value. Am I close? > > > Does this example help? > > > zvalues = [1] # starting value > increments = [0.01, 0.01, 0.02, 0.01, 0.01, 0.02, 0.01, 0.01] > for h in increments: > z = zvalues[-1] + h > zvalues.append(z) > > print(zvalues) > > > (Note: beware of floating point rounding.) > > > > > -- > Steven > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do loop in Python
Thank you so much. This script and all information was totally helpful and actually helped me for the next step of my work as well. Have a great time. Sue On Fri, Nov 25, 2011 at 10:44 PM, Andreas Perstinger wrote: > On 2011-11-25 14:46, stm atoc wrote: >> >> Here is the new version of the program: >> >> zvalues = [-200] # starting value >> hvalues = [10] # starting value >> increments = [1, 1, 1, 1, 1, 1, 1, 1] >> for N in increments: >> h = hvalues[-1] - N >> hvalues.append(h) >> z = zvalues[-1] + h >> zvalues.append(z) >> height = arange((z)*dz,0,dz) > > > There is no "arange" in python. Could it be that you use numpy and import it > with "from numpy import *"? > >> for z,when in enumerate(height): > > > I'm pretty sure this line doesn't do what you expect it to do. You have a > sequence (a numpy array) named "height" and after calling "enumerate" you > get a list of tuples in the form of [(0, height[0]), (1, height[1]), ...]. > Now the for-loop iterates over this list and assigns "z" to the first value > of the tuple (the index-values) and "when" to the second (the values from > "height"). You later never use "when" but just use "z". If you really want > that, the "enumerate" is completly unnecessary and you could just use "for z > in range(len(height))". But I'm not sure if numpy arrays work with "len()". > > >> nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence >> diffusivity m**2/s >> nu.append(num + nuh[z]) >> >> The story is like this: >> I should define layers and thickness and see how the diffusion profile >> changes over the z. >> height (or depth) of the total thickness or 'z'. >> I basically, define 'z' in 10 layers and each layer is called ' N' . >> Difference between each layer is 'h', which is equal 10 micrometer. >> Now, what I like to do is the modification of nu based on each zvalue >> In fact, for each 'zvalue' o'z' step, I need to calculate a different >> value for 'nu' based on the available equation in the program. >> >> BUT, I am not sure, exactly, how to add the new do loop of z inside >> another loop of nu. > > > For me your explanations are still too confusing. Could it be that you are > thinking way too complicated? > > My guess is you want to have a range of material thicknesses (from 1 to 200 > micrometers in 10 micrometer-steps) and then you want from each thickness 10 > different layers, right? > > import math # you should always tell us which modules you import > num = 0.05 # some constant > nu = [] # list of resulting values > h = 10.0 # height of one layer > thickness = range(0, 210, 10) # a list from 0 to 200 with step 10 (0, 10, > 20, ..., 190, 200) > layers = range(1,11) # a list from 1 to 10 > for t in thickness: > for l in layers: > z = t + h * l # I'm not sure if you want to add or subtract the layer > thickness > nu = num + (0.01 * math.exp(-0.05 * (z + 200.0))) > > This will result in a big one-dimensional list where you calculate for each > thickness the nu-value for 10 layers. Am I close? > I'm still not sure about the steps and the height of the layers. I also > wonder if it wouldn't be better to use a two-dimensional list. > > >> I have done this way as well (the other way around): >> >> height = arange((z)*dz,0,dz) >> for z,when in enumerate(height): >> for N in increments: >> h = hvalues[-1] - N >> hvalues.append(h) >> z = zvalues[-1] + h >> zvalues.append(z) >> nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence >> diffusivity m**2/s >> nu.append(num + nuh[z]) >> >> but still no sign of 'nu changes' over 'z'! > > > As Charles has already mentioned, the values for "nu" are very similar (they > start beginning to differ just at the seventh digit after the comma). How do > you further process this values? If you plot them what's your scale? > > Bye, Andreas > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] plotting in python
Hi there, I have a question regarding plotting with Python. I have the following python script: # coding: utf-8 from pylab import * import numpy filename='ourtest_out.list' fh=open(filename) line=fh.readline() fh.close z=array([ float(val) for val in line.split()[1:] ]) a = numpy.loadtxt(filename,skiprows=3) N=100 t = a[:,0] nu = a[0:,1:N+1] #Conc = a[1:,N+1:] Conc = a[1:,N+1:] levels=arange(-10,1) levels=levels[-3]-levels t=t/360. figure() plot(Conc[0],z) xlabel('C') ylabel('z') #show() savefig('Conc.png') close() #nu figure() lw = 2.0 #linewidth dpi = 96 levels=arange(-10,1) levels=levels[-3]-levels plot(nu[0],z) xlabel('nu') ylabel('z') savefig('nu.png') close() However, once I run the program (run.py) I have error like this: --- ValueErrorTraceback (most recent call last) /Users/…./run.py in () 24 25 figure() ---> 26 plot(Conc[0],z) 27 28 xlabel('C') /Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/matplotlib/pyplot.py in plot(*args, **kwargs) 2284 ax.hold(hold) 2285 try: -> 2286 ret = ax.plot(*args, **kwargs) 2287 draw_if_interactive() 2288 finally: /Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/matplotlib/axes.py in plot(self, *args, **kwargs) 3781 lines = [] 3782 -> 3783 for line in self._get_lines(*args, **kwargs): 3784 self.add_line(line) 3785 lines.append(line) /Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/matplotlib/axes.py in _grab_next_args(self, *args, **kwargs) 315 return 316 if len(remaining) <= 3: --> 317 for seg in self._plot_args(remaining, kwargs): 318 yield seg 319 return /Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/matplotlib/axes.py in _plot_args(self, tup, kwargs) 292 x = np.arange(y.shape[0], dtype=float) 293 --> 294 x, y = self._xy_from_xy(x, y) 295 296 if self.command == 'plot': /Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/matplotlib/axes.py in _xy_from_xy(self, x, y) 232 y = np.atleast_1d(y) 233 if x.shape[0] != y.shape[0]: --> 234 raise ValueError("x and y must have same first dimension") 235 if x.ndim > 2 or y.ndim > 2: 236 raise ValueError("x and y can be no greater than 2-D") ValueError: x and y must have same first dimension ---So, What would you suggest? Thanks in advance, Sue ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do loop in Python
Yes. Actually, I have changed it to this kine od script: # == model loop == #Optione1 if True: z=zeros( (numlayers,) ) thickness= (thickness*1.0) for l in layers: z = arange ((-thickness - h * l),0,dz) ##z= t -h * l nu = num+ (0.001*exp(-0.005*(z+200.))*dz) #Option2 if False: thickness = range(-200 , 0, 10) # a list from -200 to 0 with step 10 (0, 10, 20, ..., 190, 200) layers = range(1,11) # a list from 1 to 10 for t in thickness: for l in layers: z = arange(( t + h * l ), 0, dz ) #zvalues = arange(-200.,0,dz) nu = num+ (0.001*exp(-0.005*(z+200.))) plot(nu,z) Then it seems it works. it should have a trend to reducing values... - Show quoted text - On Tue, Nov 29, 2011 at 2:00 PM, Steven D'Aprano wrote: > stm atoc wrote: >> >> Thank you so much for your reply. It was very helpful information and >> I used it in order to improve the program >> >> Here is the new version of the program: >> >> zvalues = [-200] # starting value >> hvalues = [10] # starting value >> increments = [1, 1, 1, 1, 1, 1, 1, 1] >> for N in increments: >> h = hvalues[-1] - N >> hvalues.append(h) >> z = zvalues[-1] + h >> zvalues.append(z) >> height = arange((z)*dz,0,dz) >> for z,when in enumerate(height): >> nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence >> diffusivity m**2/s >> nu.append(num + nuh[z]) > > > > I'm afraid I still don't know what the arange function is. Is that a > function you have written yourself? However, I can see that it doesn't > actually get used! > > You create an arange object, and call it "height". > > height = arange((z)*dz,0,dz) > > You should insert a print statement after this line to see what value height > is given, and check that it is what you expect it to be. > > Presumably height is some sort of list or sequence of values, because you > next use it in a for-loop: > > for z,when in enumerate(height): > ... > > So now we know that z takes on the values 0, 1, 2, 3, ... and when takes on > the values from height, whatever they are. But in the rest of your code, you > don't use when at all: > > nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) > > nu.append(num + nuh[z]) > > No when, hence the values from height aren't actually used. Strange. > > Also, what are dz and num? You use them both, but I can't see where they are > defined or what value they have. Likewise nuh and nu, although I can guess > they are probably lists because you append to them. > > Because I don't know what values to use, and I don't know what arange is, I > can't run your code to see what it does. So I'm reduced to guessing. > > If I take a wild stab in the dark that dz is a small number, say, 0.01, I > can see what values nuh gets: > > > py> from math import exp > py> dz = 0.01 > py> nuh = [] > py> for z in range(10): > ... nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) > ... > py> from pprint import pprint > py> pprint(nuh) > [3.6787944117144236e-06, > 3.6604463480401533e-06, > 3.6421897957152333e-06, > 3.624024298324903e-06, > 3.6059494017307832e-06, > 3.587964654059516e-06, > 3.5700696056914737e-06, > 3.5522638092495153e-06, > 3.5345468195878014e-06, > 3.5169181937806692e-06] > > Is that the sort of behaviour you expect for nuh? > > Since the nuh values are changing, num+nuh[z] should also be changing, which > implies nu should be changing. > > Unless num is so large that rounding error wipes out the nuh values. > > > > > -- > Steven > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] plotting in python
The output of the print len(Conc[0]), len(z) is 100 3600. Now I changed Conc[0] to Conc[1], and the output is: 100 100 But still I need to see from Concentration from 0. On Thu, Dec 1, 2011 at 5:26 AM, Asokan Pichai wrote: > On Thu, Dec 1, 2011 at 2:38 AM, stm atoc wrote: >> Hi there, >> >> I have a question regarding plotting with Python. >> >> I have the following python script: > > [SNIPPED] >> plot(Conc[0],z) > > [SNIPPED] >> ---So, What would you suggest? > > What is the output of > print len(Conc[0]), len(z) > > You may insert that line above the plot and see > > Asokan Pichai ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] plotting in python
For previous script that I have written, I had trouble having one plot for all data at the same time and I could see two line for Conc[0] and Conc[1] separately. Now, even I modified Conc = a[1:, N+1:] to Conc = a[0:, N+1:], still one plot is created...which is pretty good and no error! Thank you so much, Sue On Thu, Dec 1, 2011 at 12:39 PM, Andreas Perstinger wrote: > [Still top-posting :-( ] > > > On 2011-12-01 11:13, stm atoc wrote: >> >> Well, I did also change the line in the python script to this: >> >> plot(Conc[0],z,'r-',label='initial') >> plot(Conc[1],z,'b-',label='after 20s') >> >> to see both Conc[0] and [1]. > > > And did it work? > > >> I will send the output data attaches to this email ("ourtest_out.list"). >> >> I wonder if this way is fine. > > > I'm not sure about the policy regarding attachements on this list but I > think it would have been better to provide a link than attach it. > > Anyways, I've reduced your original script, did a test run and it works as > expected (at least it shows a plot): > > import numpy > import matplotlib.pyplot as pyplot > > 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[1:, N+1:] > > print len(Conc[0]) == len(z) > > pyplot.figure() > pyplot.plot(Conc[0], z) > pyplot.show() > > Do you still get an error? > > Bye, Andreas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] New plot over the old graph
Hi there, I would like to make a new plot with new data over the old one (with old/first data) in the same condition (shape, dimensions) for comparison and analysis data. With your help, I have a good script from the previous discussion: ** from pylab import * import numpy import matplotlib.pyplot as pyplot import matplotlib.mlab as mlab 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) figure() pyplot.plot(Conc[0],z,'r-',label='initial') pyplot.plot(Conc[1],z,'b-',label='after 20s') show() * 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') However, I am not sure how to add new data over this to make a graph including both new and old data simultaneously. I do appreciate for any advice. Thank you, Sue ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New plot over the old graph
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 dpi = 96 figure(figsize=(10,6),dpi=dpi) pyplot.subplot(111) 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') pyplot.grid(True) show() savefig('Conc.png') 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! 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? I am new at python and a beginner and I do have learn while I work with it... Thanks in advance, Sue On Thu, Dec 1, 2011 at 6:25 PM, Andreas Perstinger wrote: > 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 > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New plot over the old graph
I appreciated for the accurate response. I used step by step and it is running now. Thank you very much for your advice and guidance, Sue On Thu, Dec 1, 2011 at 10:01 PM, Andreas Perstinger wrote: > 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 > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] unsupported operand
Hi there, I would like to define concentration array based on time and then plot it. Here is the profile: from pylab import * import numpy import array import math tmax=1*3600 t = 1. outfile=open('ourtest_out.list','w') N = 100 Conc1 = arange([0, tmax]) outfile.close() lw = 2.0 #linewidth dpi = 96 figure(figsize=(12,6),dpi=dpi) pyplot.plot(Conc1, t,'r-',label='Conc' ) savefig('Conc.png') show() and this is the error that I got: TypeError: unsupported operand type(s) for -: 'list' and 'int' What would you suggest? Thank you, Sue ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unsupported operand
Well, I tried to check and I do not have that problem any more..but i have the clear script as follows and a couple of question regarding to this: # coding: utf-8 from pylab import * import numpy import matplotlib.pyplot as pyplot import matplotlib.mlab as mlab #tmax=360 with open("ourtest_out.list", "r") as f: t = numpy.array([float(v) for v in f.readline().split()[1:]]) for t, filename in enumerate("ourtest_out.list"): a = numpy.loadtxt("ourtest_out.list", skiprows=3) Conc=[] #Conc = a[:, 200] Conc.append(a[:,200]) #Conc = a[0:, tmax+1:] plot(Conc,t) show() My main problem is, i do try to see the plot of Conc according to time. I have a graph without any data! Basically, I have a program written in FORTRAN including conc, time, etc. calculation. The out put data shows that I do have concentration. However, i am not able to have a plot based on time. I should mention that I did have run the model (fortran) for concentration according to the number of layer (N=200) and defined the concentration like this Conc(0:N). Perhaps the reason that I am not able to see the concentration with time is because of this definition. BUT, still if I do plot based on pyplot.pcolor, I have a nice and seems correct graph. So, actually, I am not sure what is the problem for showing the regular/linear plot. I hope I made it clear now.. Would it be possible to give me some hint please? Thank you in advance, Sue On Sun, Dec 18, 2011 at 2:07 AM, Steven D'Aprano wrote: > stm atoc wrote: > >> and this is the error that I got: > > > Would you like us to guess which line causes the error? > > Please show the ENTIRE traceback. Do not retype it from memory, paraphrase > or simplify it, or otherwise change it in any way. Copy and paste it. > > >> TypeError: unsupported operand type(s) for -: 'list' and 'int' >> >> What would you suggest? > > > The code sample you showed did not include any subtractions. This hints that > perhaps you are not showing us all of the code you are actually running. The > full traceback will answer that one way or the other. > > Otherwise, I suggest you look at the line which the traceback prints. It > probably has something like "result = a - b" (or some other subtraction). > Look for the two variables: one of them is set to a list. > > > -- > Steven > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] changing coordinate
Hi: I am trying to define a new coordinate for My plot. I have a plot x axis is Concentration, Y axis, depth or Z. Z= -1000 um. and I would like to see the the trend and plot of concentration up to -300 um and see how the plot changed according to the depth. So, my main question is about how to change the coordinate based on 0 to -300. This is the relevant description: # coding: utf-8 from pylab import * import numpy import matplotlib.pyplot as pyplot import matplotlib.mlab as mlab #t=3600 N = 100 dz = 1.0 h_s=-300 with open("ourtest_out.list", "r") as f: z = numpy.array([float(v) for v in f.readline().split()[1:]]) z == z + h_s a = numpy.loadtxt("ourtest_out.list", skiprows=3) t=a[:,0] nu = a[0:,1:N+1] Conc = a[0:, N+1:] lw = 2.0 #linewidth dpi = 96 figure(figsize=(12,6),dpi=dpi) pyplot.plot(Conc[1], z,'r-') pyplot.xlabel('$Conc, mmol C m^3$') pyplot.ylabel('$Hsml, micrometer$') pyplot.grid(True) legend() savefig('Conc.png') show() lw = 2.0 #linewidth dpi = 96 figure(figsize=(12,6),dpi=dpi) semilogx(Conc[1], z,'r-') pyplot.xlabel('$Conc, mmol C m^3$') pyplot.ylabel('$Hsml, micrometer$') pyplot.grid(True) legend() savefig('semiConc.png') show() lw = 2.0 #linewidth dpi = 96 figure(figsize=(12,6),dpi=dpi) semilogx(nu[1], z,'r-') pyplot.xlabel('$nu, m^2/s^{-1}$') pyplot.ylabel('$Hsml, micrometer$') pyplot.grid(True) legend() savefig('nu.png') show() -- I do appreciate any help and advice. Thanks,Sue ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] TypeError: only length-1 arrays can be converted to Python scalars
Hi, I have a question regarding the TypeError. I have this script as follows in Python. I have run a program in Fortran: # coding: utf-8 from pylab import * import numpy import matplotlib.pyplot as pyplot import matplotlib.mlab as mlab t=3600 N = 100 dz = 1.0 with open("act_out.list", "r") as f: z = numpy.array([float(v) for v in f.readline().split()[1:]]) zm = z*1.0e6 a = numpy.loadtxt("act_out.list", skiprows=3) t=a[:,0] nu = a[0:,1:N+1] Conc = a[:, N+1:] hsml=zeros((103)) cprof=zeros((103)) hsml[0]=-1300. hsml[-2]=-300. hsml[-1]=0. hsml[1:-2]=z*100-300. cprof[0]=50. cprof[-2]=500. cprof[-1]=500. idx=int(where(t==1800)[0]) cprof[1:-2]=Conc[idx] lw = 2.0 dpi = 80 figure(figsize=(8,8),dpi=dpi) pyplot.plot(cprof,hsml,'r-') pyplot.xlabel('$Conc, mmol C m^3$') pyplot.ylabel('$Hsml, micrometer$') pyplot.grid(True) legend() savefig('Conc.png') show() after runing Python, I hav ethis massage: TypeError: only length-1 arrays can be converted to Python scalars Any suggestion? in fact, what I missed and what I should add to have a right result! Thanks in advance, Sue ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor