使用等式在python中绘图

时间:2015-01-02 03:35:57

标签: python graph

我的老师要求我们编写一个能够绘制星星图形的python程序。他给了我们这个等式" y = x2 + 3"对于范围x = 0到x = 6。 所以Python会询问x的值,程序应该通过数学运算自动输出正确位置的星号。

这是图表的外观示例:

这里,公式是y = x ^ 2:

  y

16|            *     

  |

14|

  |

12|

  |

10|

  |         *

 8|

  |

 6|

  |

 4|      *

  |

 2|

  |   *

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

   0  1  2  3  4

到目前为止,我可以为轴编写一个程序:

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",))

但我不知道如何编写一个程序,根据公式输出" *" s! 我是一个初学者,我们还没有学到#Mattlotlib"爱好。

他还给了我们这个来帮助我们:

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

如果有人可以提供帮助,那就太棒了。谢谢!

2 个答案:

答案 0 :(得分:0)

我同意Phuc Tran的评论,你可以创建一个数组然后你可以绘制数组。

另一种非正统的方法是计算给定函数的逆。由于您在 y 方向上进行迭代,因此计算 x 的值,如果是整数,则绘制它。

以下代码是您工作的修改版本,它将从 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",))

您可以使用它来思考另一个更简单的解决方案或作为Phuc Tran建议的基础。

答案 1 :(得分:0)

这是获得理想情节的一种方法。

首先使用" "*填充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)

然后只需打印出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