Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何绘制给定函数f(x)的基于文本的图形?_Python_Python 3.x_Function_If Statement_Graph - Fatal编程技术网

Python 如何绘制给定函数f(x)的基于文本的图形?

Python 如何绘制给定函数f(x)的基于文本的图形?,python,python-3.x,function,if-statement,graph,Python,Python 3.x,Function,If Statement,Graph,我正在处理一项大学计算机科学作业。我学习python编程才几个月。 以下是我任务的目标: 编写一个程序来绘制数学函数f(x)的基于文本的图形。使用-10到10的轴限制,仅绘制离散点,即具有整数值坐标的点 样本I/O: 到目前为止,我已经使用嵌套的for循环创建了图形区域和轴。这是我目前的代码(我已经注释掉了不必要或不完整的行): 我的代码生成以下内容,这是一个不错的开始: | | |

我正在处理一项大学计算机科学作业。我学习python编程才几个月。 以下是我任务的目标:

编写一个程序来绘制数学函数f(x)的基于文本的图形。使用-10到10的轴限制,仅绘制离散点,即具有整数值坐标的点

样本I/O:

到目前为止,我已经使用嵌套的
for
循环创建了图形区域和轴。这是我目前的代码(我已经注释掉了不必要或不完整的行):

我的代码生成以下内容,这是一个不错的开始:

          |       
          |      
          |    
          |    
          |   
          |  
          |  
          | 
          |
          |
----------+----------
          |
          |
          |
          |
          |
          |
          |
          |
          |
          | 
但是现在我被卡住了。。。我的问题是:如何在轴内绘制给定函数

这是我的主要想法:

  • 我需要将空白/轴替换为
    'o'
    ,其中应绘制给定函数

我将感谢任何帮助

首先定义函数,然后在绘图循环中包括o点

def f(x):
    return 2*x

for row in range (0, 21):
    for col in range (0, 21):


        if row == -f(col-10) + 10:
            print ("o", end="")
        elif row == 10 and col == 10:
            print ("+", end="")
        elif row == 10:
            print ("-", end="")
        elif col == 10:
            print ("|", end="")
        else:
            print (" ", end="")


    print()

这适用于线性函数-因为您是从左上角定义轴,而不是从中心定义轴,因此可能需要调整变换以正确绘制其他图形。

有许多方法可以剥除这只猫的皮。最紧凑的方法可能是进行函数反转(获取所有f(x),然后对结果对应的线或空网格线进行程序绘制),但它的灵活性最低,并且只能绘制线性函数。正如TMrtSmith的回答所示,您尝试的程序性方法也极为低效

这可能是一种更好的形式,可以将网格创建和绘图拆分为单独的过程-这也可以通过多种方式完成,但下面的一种方式将使您能够不依赖标准输出。生成网格非常简单:

grid = []  # we'll use a list to store the individual rows
for y in range(-10, 11):
    if y != 0:
        grid.append("|".center(21))
    else:
        grid.append("+".center(21, "-"))
现在,您只需通过替换每个匹配网格线上的字符,在该网格上绘制函数:

for x in range(-10, 11):
    value = int(f(x))  # get the function result as int, you can call any other function of course
    if -10 <= value <= 10:  # no reason to plot outside of our grid range
        x = x + 10  # normalize x to the grid
        y = 10 - value  # normalize y to the grid
        grid_line = grid[y]
        grid[y] = grid_line[:x] + "o" + grid_line[x+1:]
或者,如果要将其保留为字符串,可以在网格中添加新行,然后打印、保存、发送,然后使用它执行任何操作:

baked_grid = "\n".join(grid)
print(baked_grid)
它不仅会绘制线性函数,如:

def f(x):
    return x + 2
导致:

          |       o  
          |      o   
          |     o    
          |    o     
          |   o      
          |  o       
          | o        
          |o         
          o          
         o|          
--------o-+----------
       o  |          
      o   |          
     o    |          
    o     |          
   o      |          
  o       |          
 o        |          
o         |          
          |          
          |          
但也适用于非线性函数,例如:

import math

def f(x):
    return math.sqrt(abs(10 - x * x)) - 5
结果:

          |          
          |          
          |          
          |          
          |          
          |          
o         |         o
 o        |        o 
  o       |       o  
   o      |      o   
----o-----+-----o----
     o    o    o     
      o oo|oo o      
          |          
       o  |  o       
          |          
          |          
          |          
          |          
          |          
          |          

另外,它还可以与Python 2.6+配合使用,尽管我建议您在这种情况下使用
xrange
而不是
range
,以获得最大的效率。

感谢您的回复,他们对我很有帮助。我用以下代码完成了该程序:

import math

function = input("Enter a function f(x):\n")

for y in range (10, -11, -1):
    for x in range (-10, 11):
        f = round(eval(function))
        if x == 0 and y == 0 and y != f:
            print ("+", end="")
        elif y == 0 and y != f:
            print ("-", end="")
        elif x == 0 and y != f:
            print ("|", end="")
        elif y == f:
            print ("o", end="")
        else:
            print (" ", end="")
    print()
我添加了另一个
elif
语句来打印
o
,其中函数
f
等于
y
。我还通过使用
y!=在
if
语句中的x

我将变量
col
row
分别更改为
x
y
(谢谢@zwer)。我还从中间重新定义了我的图,而不是从左上角,这样它就可以处理非线性函数(谢谢@TMrtSmith)

我不确定这是否是最有效的方法,但它是有效的@zwer,我没能让你的代码正常工作(我认为这有点超出我目前的技能水平),但它还是有帮助的

输入
x**2
返回:

          |          
       o  |  o       
          |          
          |          
          |          
          |          
        o | o        
          |          
          |          
         o|o         
----------o----------
          |          
          |          
          |          
          |          
          |          
          |          
          |          
          |          
          |          
          |          

问题解决了!谢谢

您可以将一个函数作为参数传递给方法,该方法执行x=-10到10的函数,将结果舍入为整数并返回结果列表,然后您将得到一个y值列表,如果块缩进出错,您可以绘制该列表。此外,这种绘制数据的方法效率极低-您正在为图表上的每个点调用
f(x)
,只是为了确定当前点是否应打印
o
或其他字符。修复了if块。@zwer OP正在就其函数寻求帮助,这就是我的回答。在这种情况下,这只是比他已经做的多了一张支票。
          |          
          |          
          |          
          |          
          |          
          |          
o         |         o
 o        |        o 
  o       |       o  
   o      |      o   
----o-----+-----o----
     o    o    o     
      o oo|oo o      
          |          
       o  |  o       
          |          
          |          
          |          
          |          
          |          
          |          
import math

function = input("Enter a function f(x):\n")

for y in range (10, -11, -1):
    for x in range (-10, 11):
        f = round(eval(function))
        if x == 0 and y == 0 and y != f:
            print ("+", end="")
        elif y == 0 and y != f:
            print ("-", end="")
        elif x == 0 and y != f:
            print ("|", end="")
        elif y == f:
            print ("o", end="")
        else:
            print (" ", end="")
    print()
          |          
       o  |  o       
          |          
          |          
          |          
          |          
        o | o        
          |          
          |          
         o|o         
----------o----------
          |          
          |          
          |          
          |          
          |          
          |          
          |          
          |          
          |          
          |