By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,420 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,420 IT Pros & Developers. It's quick & easy.

How can i plot a graph using values stored in two different lists

P: 1
I am trying to plot a graph that takes a range of xvalues stored in a list and a function 'y' that takes those values as one of its operand . 'y' values are also stored in another array for each of xvalues.

I am generating a range of xvalues through random function, that will be stored in a list . For each xvalue i am calculating log value and storing it in list z . Another function : -Ta*log(xvalues)

Finally i need to plot graph for xvlaues in list 'a' and function values in list 't'

*********************** plot.py ***************************
Expand|Select|Wrap|Line Numbers
  1.  
  2. import matplotlib.pyplot as plt
  3. import math
  4.  
  5. a=[0]*6
  6. z=[0]*6
  7. b=[0]*6
  8. t=[0]*6
  9.  
  10.  
  11. import random
  12. for i in range(5):
  13.   x=random.random()
  14.   y=3*x
  15.   a[i]=y                
  16.   z[i]=math.log(a[i])
  17.  
  18.  
  19. i = 0
  20. while i <=5:
  21.   if i==0:
  22.     i=i+1
  23.     continue
  24.   else:
  25.     b[i]=a[i]-a[i-1]
  26.   i=i+1
  27.  
  28.  
  29. s=0
  30. i=0
  31. while i<=5:
  32.   s=s+b[i]
  33.   i=i+1
  34.  
  35.  
  36. Ta = s/6
  37. for i in range(5):
  38.   t[i]=(-Ta)*z[i]
  39.  
  40. plt.plot(a,t)
  41. plt.show()
  42.  
  43.  
************************************************** *********
Apr 8 '12 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 626
zip is one of several ways if the plot code expects a list of x,y points.
Expand|Select|Wrap|Line Numbers
  1. a=[1, 2, 3, 4, 5]
  2. b=[10, 20, 30, 40, 50]
  3. print zip(a, b) 
Also the while loop is a little clumsy with the test for zero and the continue statement. You can replace it with a for() loop
Expand|Select|Wrap|Line Numbers
  1. for i in range(1, 5):
  2.     b[i]=a[i]-a[i-1] 
Apr 9 '12 #2

P: 2
Assuming you are using matplotlib to produce your graphs, see the 3rd example on

http://matplotlib.sourceforge.net/us..._tutorial.html

- The example with 3 sets of data on one graph (red dashes, blue squares and green triangles).

pyplot does accept multiple lists of y values as a single argument, but you'd need to test it to see how that works out. Given your situation, I think you should stick to the example's use of separate arguments for each list of y values.

I suspect things could also be made to work by calling pyplot multiple times and calling show just once. But I'm uncertain if pyplot will rescale the axes as needed if you toss numbers at it that don't fit on the scale picked for the earlier plots. I think it can autoscale, but without testing, I'm not sure how much you have to tell it for it to do the right thing.

If I may sound a louder cautionary note along the lines of dwblas's complaint that there's something funny about your while loops, I want you to note that his

for i in range(1,5):

runs with i=1, then 2, 3, 4 and then stops iterating. Your original loop ran i=0 (with special case code to make it a no-op), then 1, 2, 3, 4, 5.

I'm assuming you specifically wanted b[0] to be left with a value of 0, but if you really want b[5] to be set, the loop suggested by dwblas needs adjusting.

Sprinkling 5's and 6's throughout your code is setting yourself up for a maintenance nightmare. At a slight runtime cost, I suggest that you'll be much happier in the long run with code like

a=[0]*6 # Comment to explain why you picked 6 here
b=[0]*len(a)

Note that as coded above it is clear that however long a is, b is supposed to be the same length.

Similarly,

for i in range(len(a)):

will iterate for each index to the a list and will still be right even if you decided on a different length for a in some future revision of the code.

Want to skip a[0]? Then:

for i in range(1,len(a)):

will do it.

Clean coding counts in the long run!
Apr 26 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.