1 Replies - 1298 Views - Last Post: 20 March 2013 - 04:02 AM Rate Topic: -----

#1 uzerza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-March 13

drawing an ASCII graph using python loops

Posted 20 March 2013 - 02:00 AM

Hi,

Im trying to make an ASCII graph using python loops.
The goal is for the user to enter an input (formula) [x+1] etc. and print out the graph
the domain and range of the graph only goes from -10 to +10
I managed to get it working, but only for the formula (-x+1), which is (x+1) in my code
I know this problem is because my loop starts printing it from the top, so I would have to change it so it prints the loop in the negative direction but I am stuck on how to combine my graph into a nested loop so it prints it out properly, and then replaces the corresponding place of the function output.


I can give you any more information if you need.

Any constructive thoughts would be much appreciated.



height=20
w=10
x=0
for a in range(0,11):
	ps= ((" " * w)+('|')+(" " * w))
	ps=list(ps)
	ps[x]="*"
	x=x+1
	pp="".join(ps)
	print(pp)
	
	
	
rs=(('-'*10)+('+')+('-'*10))
rs=list(rs)
rs[x]="*"
rr="".join(rs)
print(rr)
 	
for a in range(0,10):
	ws = ((" " * w)+('|')+(" " * w))
	ws=list(ws)
	y=x+1
	ws[y]="*"
	x=x+1
	wp="".join(ws)
	print(wp)
	
	



For anyone too lazy to enter in the code the output looks something like:

*         |          
 *        |          
  *       |          
   *      |          
    *     |          
     *    |          
      *   |          
       *  |          
        * |          
         *|          
          *          
----------+*---------
          | *        
          |  *       
          |   *      
          |    *     
          |     *    
          |      *   
          |       *  
          |        * 
          |         *


(its not showing it correctly on the site, but that is supposed to be a graph of (-x+1), It displays fine when using an interpreter)

This post has been edited by baavgai: 20 March 2013 - 03:26 AM
Reason for edit:: tagged graph, shows better


Is This A Good Question/Topic? 0
  • +

Replies To: drawing an ASCII graph using python loops

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5840
  • View blog
  • Posts: 12,694
  • Joined: 16-October 07

Re: drawing an ASCII graph using python loops

Posted 20 March 2013 - 04:02 AM

You're kind of faking yourself out with this. The ascii bars are secondary to the plotted points.

I'd start with something like:
def f(x): 
	return x-1 # y = x + 1
	
r = range(-10,11)
for y in r:
	line = []
	for x in r:
		# your code here
	print(''.join(line))



Your logic should be if f(x)==y then plot, otherwise choose an appropriate "empty" char. If (x,y)==(0,0) then that char would be "+". If it's none of your three special cases, it's ' '.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1