【问题标题】:How do i change the values on the axes in the plot using tkinter and matplotlib?如何使用 tkinter 和 matplotlib 更改绘图中轴上的值?
【发布时间】:2020-11-16 11:19:25
【问题描述】:

我正在使用 tkinter 创建一个包含两条曲线的图。我设法绘制了它们,但它仅在 x 轴上显示正值。

    graph = Figure(figsize=(5,5), dpi=100)
    A0=1
    b=.1

    y = [-((A0/pi)*(1-b*x))**0.5 for x in range(-101, (int(1/b)+1))]
    z = [((A0 / pi) * (1 - b * x)) ** 0.5 for x in range(-101, (int(1/b)+1))]

    plot1 = graph.add_subplot(111)
    plot2 = graph.add_subplot(111)

    plot1.plot(y)
    plot2.plot(z)

    canvas1 = FigureCanvasTkAgg(graph, master=self)
    canvas1.draw()
    canvas1.get_tk_widget().grid(row=0, column=0, columnspan=5 )

如何使这些值成为创建曲线期间使用的 x 值?

【问题讨论】:

  • 如果您可以将您的代码转换为Minimal, Reproducible Example 将会很有帮助,这是一段我们可以运行而无需编辑它并猜测导入语句是什么的代码。

标签: python-3.x matplotlib tkinter tkinter-canvas


【解决方案1】:

您需要同时将 x 和 y 传递给 plot()。您还可以使用numpy 简化和加速您的代码。此外,您不需要创建两个子图。

x = np.arange(-101, (int(1/b)+1))
y = -((A0/np.pi)*(1-b*x))**0.5
z = ((A0 /np.pi) * (1 - b * x)) ** 0.5

plot1 = graph.add_subplot(111)

plot1.plot(x,y)
plot1.plot(x,z)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-15
    • 2013-09-12
    • 2011-06-13
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多