输入3次并生成3个图形功能

发布于 2025-02-10 02:18:11 字数 434 浏览 5 评论 0原文

首先,感谢您的帮助。 正如我在标题上所说的,我想进行3次输入并生成3个图表功能

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

times = range(1, 1)

for i in times:
   
    def my_func2(x):
        a=int(input())
        return x**a

        x=np.linspace(0,2)
        y=my_func2(x)

        plt.plot(x, y)
        plt.xlabel("x",size=14)
        plt.ylabel("y",size=14)
        plt.grid()
        plt.show()
    i=i+1

First of all Thanks for your help.
As I said at title, I want to make input 3 times and generate 3 graphs function

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

times = range(1, 1)

for i in times:
   
    def my_func2(x):
        a=int(input())
        return x**a

        x=np.linspace(0,2)
        y=my_func2(x)

        plt.plot(x, y)
        plt.xlabel("x",size=14)
        plt.ylabel("y",size=14)
        plt.grid()
        plt.show()
    i=i+1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请止步禁区 2025-02-17 02:18:11

您的凹痕错误 - 这改变了Python中的一切。您尝试在my_func2()内绘制y = y = my_func2(x)内部 def my_func2(x): - 因此您永远不会运行此功能。

如果要重复3次,请使用range(3)而不是range(1,1)且没有i = i = i + 1

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

# --- functions ---

def my_func2(x):
    a = int(input())  
    return x**a

# --- main ---

for i in range(3):
    x = np.linspace(0,2)
    y = my_func2(x)

    plt.plot(x, y)
    plt.xlabel("x", size=14)
    plt.ylabel("y", size=14)
    plt.grid()
    plt.show()

但是我会没有功能

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

# --- main ---

for i in range(3):
    a = int(input())  

    x = np.linspace(0,2)
    y = x**a

    plt.plot(x, y)
    plt.xlabel("x", size=14)
    plt.ylabel("y", size=14)
    plt.grid()
    plt.show()

You have wrong indentations - and this change everything in Python. You try to plot inside my_func2() and you run y=my_func2(x) inside def my_func2(x): - so you never run this function.

And if you want to repeat 3 times then use range(3) instead of range(1, 1) and without i = i + 1

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

# --- functions ---

def my_func2(x):
    a = int(input())  
    return x**a

# --- main ---

for i in range(3):
    x = np.linspace(0,2)
    y = my_func2(x)

    plt.plot(x, y)
    plt.xlabel("x", size=14)
    plt.ylabel("y", size=14)
    plt.grid()
    plt.show()

But I would do it without function

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

# --- main ---

for i in range(3):
    a = int(input())  

    x = np.linspace(0,2)
    y = x**a

    plt.plot(x, y)
    plt.xlabel("x", size=14)
    plt.ylabel("y", size=14)
    plt.grid()
    plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文