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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vk2012
    New Member
    • Apr 2012
    • 1

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

    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 *************** ************
    Code:
    import matplotlib.pyplot as plt
    import math
    
    a=[0]*6
    z=[0]*6
    b=[0]*6
    t=[0]*6
    
    
    import random
    for i in range(5):
      x=random.random()
      y=3*x
      a[i]=y                
      z[i]=math.log(a[i])
    
    
    i = 0
    while i <=5:
      if i==0:
        i=i+1
        continue
      else:
        b[i]=a[i]-a[i-1]
      i=i+1
    
    
    s=0
    i=0
    while i<=5:
      s=s+b[i]
      i=i+1
    
    
    Ta = s/6
    for i in range(5):
      t[i]=(-Ta)*z[i]
      
    plt.plot(a,t)
    plt.show()
    *************** *************** *************** **************
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    zip is one of several ways if the plot code expects a list of x,y points.
    Code:
    a=[1, 2, 3, 4, 5]
    b=[10, 20, 30, 40, 50]
    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
    Code:
    for i in range(1, 5):
        b[i]=a[i]-a[i-1]

    Comment

    • rdrewd
      New Member
      • Sep 2007
      • 2

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



      - 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!
      Last edited by rdrewd; Apr 26 '12, 07:49 AM. Reason: Fix typo.

      Comment

      Working...