python - 你如何在 matplotlib 的同一张图上有多个图
问题描述
下面的代码是这个 Matplotlib 示例的网格,用于嵌入到 Tk 中,我复制了这个类 以使工具栏垂直(它在另一个文件中,但我保证是逐行),以及来自文本框输入的这个 Matplotlib 示例的灵感
我试图让每个文本框在图表上为所写的任何数学表达式绘制一个新图。我让它与 Matplotlib 的文本框小部件一起工作,但它不允许我将它们放在图形画布之外,所以我使用了 Tk Entry 小部件。目前只有最底部的文本框会真正更新图形(尝试写 x 或 x^2 来查看)。既然我正在使用 Tk Entry 小部件,为什么其他的不能工作
### - Imports - ###
from VerticleToolbar import VerticalNavigationToolbar2Tk as VToolbar
import numpy as np
import tkinter
import wx
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
### - GUI Setup - ###
#Create window and set metadata
root = tkinter.Tk()
root.wm_title("Matplotlib Grapher")
root.geometry("842x400")
#create side panel for textboxes
frame = tkinter.Frame(root, borderwidth=2, relief="solid", width=500)
#Create matplotlib canvas
fig = Figure(figsize=(5, 4), dpi=100)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
#Create Matplotlib toolbar
toolbar = VToolbar(canvas, root)
toolbar.update()
### - Textbox Logic - ###
class text_input:
def __init__(self):
self.x = np.arange(-2.0, 2.0, 0.001)
self.l, = fig.add_subplot(111).plot(self.x, np.zeros_like(self.x), lw=2)
self.string_value = tkinter.StringVar()
self.string_value.set('')
self.string_value.trace_add("write", self.submit)
self.text_box = tkinter.Entry(frame, textvariable=self.string_value, width=50)
self.text_box.grid(column=1)
def submit(self, mode: str, idkbro: str, idkbro2: str):
expression = self.string_value.get()
expression = expression.replace("x", "self.x")
expression = expression.replace('^', "**")
self.ydata = eval(expression)
self.l.set_ydata(self.ydata)
canvas.draw()
### - Running GUI - ###
inputs = []
for n in range(5):
inputs.append(text_input())
frame.grid( row=0, column=1, ipadx=0, ipady=0, padx=0)
canvas.get_tk_widget().grid(row=0, column=2, ipadx=0, ipady=0, padx=0)
toolbar.grid( row=0, column=3, ipadx=0, ipady=0, padx=0)
tkinter.mainloop()
有效的代码如下:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
class text_input:
def __init__(self, height):
self.x = np.arange(-2.0, 2.0, 0.001)
self.l, = ax.plot(self.x, np.zeros_like(self.x), lw=2)
self.axbox = fig.add_axes([0.1, height, 0.8, 0.075])
self.text_box = TextBox(self.axbox, "y =")
self.text_box.on_submit(self.submit)
def submit(self, expression: str):
expression = expression.replace("x", "self.x")
expression = expression.replace('^', "**")
self.ydata = eval(expression)
self.l.set_ydata(self.ydata)
ax.relim()
ax.autoscale_view()
plt.draw()
inp_1 = text_input(0.93)
inp_2 = text_input(0.85)
inp_3 = text_input(0.09)
inp_4 = text_input(0.01)
plt.show()
我认为这与线条有关
ax.relim()
ax.autoscale_view()
因为我不确定如何在没有 PyPlot 的情况下复制它们
解决方案
推荐阅读
- r - 填充 R 数据框中的缺失值而不丢失行名和列名?
- c++ - 为什么我不能插入一个指向多重集的 const 指针?
- java - AndroidManifest.xml 中的包“com.class.xxx.test”不是有效的 Java 包名称,因为“类”是 Java 关键字
- sql - 使用不同的表更新表
- javascript - 如何将递归回调函数转换为异步等待格式
- php - 如何让我的会话 ID 正常工作
- vb.net - ClickOnce 默认安装路径
- android - 在 java 中的 openalpr 库中出现错误
- python - 无法在python的打字函数中传递变量
- sql - 在重复键上给出无效的令牌