简体   繁体   English

使用公式在python中绘图

[英]Graphing in python using an equation

My teacher asked us to write a python program that will graph stars. 我的老师要求我们编写一个可以绘制星图的python程序。 He gave us this equation "y=x2+3" for the range x = 0 to x = 6. So Python will ask for the value of x and the program should automatically output the stars in the right places by doing the math. 他为我们提供了等式“ y = x2 + 3”,其范围为x = 0到x =6。因此Python将要求x的值,并且程序应通过数学运算自动在正确的位置输出星星。

It's an example of how the graph should look like: 这是图形外观的一个示例:

Here, the formula is y=x^2: 在这里,公式为y = x ^ 2:

  y

16|            *     

  |

14|

  |

12|

  |

10|

  |         *

 8|

  |

 6|

  |

 4|      *

  |

 2|

  |   *

   ------------- X

   0  1  2  3  4

So far i could write a program for the axis: 到目前为止,我可以为轴编写程序:

print ("{0:>4}".format ("y"))
print ()
for counter in range (40,-1,-2):
    print ("{0:>4}".format ("|"))
    print ("{0:>2}".format (counter))

for counter in range (1,2):
    print ("{0:>32}".format("------------------------- X"*counter))
    print ("{0:>6}{1:>4}{2:>4}{3:>4}{4:>4}{5:>4}{6:>4}" .format("0", "1", "2", "3", "4", "5", "6",))

but i don't know how to write a program that will output the "*"s based on the equation! 但是我不知道如何编写一个基于等式输出“ *”的程序! I'm a beginner and we haven't learned "Matplotlib" yet. 我是一个初学者,我们还没有学会“ Matplotlib”。

He also gave us this to help us: 他还给了我们这个帮助我们:

for count in range(1,60,3):
    myWidth = count
    myCharacter = '*'
    print('{0:>{width}}'.format(myCharacter, width=myWidth))

It'd be great if anyone can help. 如果有人可以帮助,那就太好了。 Thank you! 谢谢!

I agree with the comment of Phuc Tran , you could create an array and then you can plot the array. 我同意Phuc Tran的评论,您可以创建一个数组,然后可以绘制该数组。

Another non orthodox way for doing it is to calculate the inverse of the given function. 另一非常规方法是计算给定函数的逆函数。 Due that you're iterating in the y direction, calculate the value for x and, if it is an integer, plot it. 由于要在y方向上进行迭代,因此请计算x的值,如果是整数,则将其绘制出来。

The following code is a modified version of your work that will do that, it iterates from the top value of y , calculates the inverse of the function and plots the x if it is an integer: 以下代码是您工作的修改版本,它将从y的最高值开始进行迭代,计算该函数的反函数并绘制x(如果它是整数):

#!/usr/bin/env python

import math

print ("{0:>4}\n".format ("y"))

for counter in range (40,-1,-1):
    # Calculate the inverse of the equation, i.e, given a Y value, 
    # obtain the X 
    y = counter
    x = math.sqrt(y)

    # Now, if x is an integer value, we should output it (floating values won't
    # fit in the ascii graph)
    # To know if x is integer, you can either use the following trick 
    # 
    #    x == int(x)    
    # 
    # Or you can use the is_integer function (Here we'll be using this one)
    if x.is_integer(): 
        # Here we calculate the number of spaces to add before outputing the '*'
        #
        # (there is a small issue with the space count, they are not
        # properly aligned, but I hope you can come up with solution for it ;) )
        spaces = "   " * int(x)
        print ("{0:>2}{1:>4}{2}*".format(counter if counter % 2 == 0 else "", "|", spaces))
    else:
        # This will fix your y axis so it will look like the asked format. It 
        # just ignores the counter if it is odd and keeps the space to add the '|'
        print ("{0:>2}{1:>4}".format(counter if counter % 2 == 0 else "", "|"))         

for counter in range (1,2):
    print ("{0:>32}".format("------------------------- X" * counter))
    print ("{0:>6}{1:>4}{2:>4}{3:>4}{4:>4}{5:>4}{6:>4}" .format("0", "1", "2", "3", "4", "5", "6",))

You can use this to think in another simpler solution or as a base for Phuc Tran suggestion. 您可以使用它来思考另一个更简单的解决方案,或者作为Phuc Tran建议的基础。

Here's one way to get the desired plot. 这是获得所需图的一种方法。

First fill a 2D array with " " and * : 首先用" "*填充2D数组:

x = # whatever value
y = x*x + 3
coord = []
y_ax = y

for y_ax in range(y, -1, -1):
    pts = []
    for x_ax in range(1, x+1):
        eq = x_ax*x_ax + 3
        if y_ax == eq:      
            pts.append("*")
        else:
            pts.append(" ")
    coord.append(pts)

Then simply print out the 2D array: 然后只需打印出2D数组:

label = y
for row in coord:
    if "*" in row:
        print('{:<2}|'.format(label), end=" ")
    else:
        print('{:<2}|'.format(""), end=" ")
    for col in row:
        print(col, end="")
    print(end="\n")
    label-=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM