473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
2 2382
dwblas
626 Expert 512MB
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
rdrewd
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

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

Similar topics

1
by: Soon Lee | last post by:
any one know of any good way to plot graph using vb.net?
11
by: srinivas | last post by:
Hi all, I have one requirement.Is there any way to create a line graph using javascript.If it is please send me the sample code.But the thing is it should work in all browsers. Thanks,...
5
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
0
by: Syazwin | last post by:
Hye everyone! :) i am really a new learner in vb.net. not so expert about what i am searching now.. actually i am looking for the way/source code how to make a line graph in vb.net. the system...
0
by: suedasszyy | last post by:
Haiiii.... i'm new for VB.net. can someone help me to solve those question? is it possible to draw a graph such as Sin graph using console application? if not, how can i draw a graph...
3
by: ray | last post by:
Hi, I would like to ask how to plot graph in vb.net on the interface? Is it possible? Thanks a lot, Ray
12
by: slowgirl | last post by:
i'm using vb to plot graph. i've already drawn the graphsheet using drawlines. and include labels. all i arrange according to pixels. the problem i face now is that i need to plot certain symbols...
1
by: sainiamit25 | last post by:
Hi, I need to make a graph using javascript based on the values in the oracle table. Can somebody throw some on it? Thanks in advance for your help, Amit
6
by: gubbachchi | last post by:
Hi all, I have got a problem with plotting bar graph using GD. I am learning GD now. I have written the code for basic bar graph with one bar. What I need is, scale on Y-axis i.e. divisions on...
0
by: Andy B | last post by:
I am using the default XML provider for the sitemap. I need the sitemap to display in different lists seperated based on node. Here is an example sitemap that I have. in web.sitemap (the list of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.