drawing an ASCII graph using python loops

Page 1 of 1

1 Replies - 4685 Views - Last Post: 20 March 2013 - 04:02 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316054&amp;s=def499f7c582ad1146d991663472743c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 uzerza

Reputation: 0
• 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.

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

• Dreaming Coder

Reputation: 7163
• Posts: 14,929
• 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.

```def f(x):
return x-1 # y = x + 1

r = range(-10,11)
for y in r:
line = []
for x in r: