有没有办法将现有的 X-Y 图转换为 X-Y-Y1 图(重新填充)?

问题描述 投票:0回答:1

我有一个 X-Y 图,我想用另一个 Y1 重新填充同一图。基本上将现有的 X-Y 图转换为 X-Y-Y1 图。因为我想自动化并将多个 Y 添加到同一个图中,所以我认为初始化一个空的 int 列表就可以解决问题。以下代码是我的尝试:

import numpy as np
import matplotlib.pyplot as plt
from tkinter import *
from matplotlib.backends.backend_tkagg import (
     FigureCanvasTkAgg, NavigationToolbar2Tk)

class Program_Class:
    
    def __init__(self, master):

        # Main Window parameters
        Main.resizable(0,0)
        Main.geometry("900x550")
        Main.title("Main")

        # Creating a figure object with transparency
        transparent = (0, 0, 0, 0)
        plot_2D = plt.figure(figsize=(2,2), facecolor=transparent)

        # Creating axes object on top of plot_2D figure
        self.axes = plot_2D.add_axes([0.1, 0.1, 0.8, 0.8])

        # Populating x axis with data
        dt = 1
        t = np.arange(0, 30, dt)
        self.x = t

        # Populating Y axis with data
        self.y = np.sin(2 * np.pi * 0.2 * t)

        # Populating Y1 axis with data
        self.y1 = np.sin(2 * np.pi * 5 * t) 

        # Creating an empty list to store X, Y
        self.axes_plot = [[None] for _ in range(4)]

        # Storing the data in a vector 
        self.axes_plot[0] = self.x
        self.axes_plot[1] = self.y

        # Populatind the axes with the extracted data
        self.axes.plot(self.axes_plot, linewidth=2.0) # <----- here it give me an error
        # ValueError: Input could not be cast to an at-least-1D NumPy array

        # Drawing the object figure on top of Main Window
        color = '#%02x%02x%02x' % (196, 233, 242)
        self.Canvas_plot2D = FigureCanvasTkAgg(plot_2D, Main)
        self.Canvas_plot2D.get_tk_widget().config(bg=color, width = 400, height=300)
        self.Canvas_plot2D.get_tk_widget().place(x=300,y=71)
        self.Canvas_plot2D.draw()

        # Creating a button for adding another Y axes
        extract_2d_button=Button(Main, text="Add Y1", width=10, heigh=2, command = self.add_Y1)
        extract_2d_button.place(anchor='nw',x=10, y=460)

    # My attempt at adding another Y axis 
    def add_Y1(self):

        # Updating the list with Y1
        self.axes_plot[2] = self.x
        self.axes_plot[3] = self.y1

        # Repopulating the plot
        self.axes.plot(self.axes_plot, linewidth=2.0)
        self.Canvas_plot2D.draw()



Main = Tk()
Program_Class(Main)
Main.mainloop()

如有任何帮助,我们将不胜感激:)

python tkinter plot axis
1个回答
0
投票

修改

self.axes.plot
调用以将
self.axes_plot[0]
self.axes_plot[1]
作为单独的参数传递,而不是将
self.axes_plot
作为单个参数传递:

self.axes.plot(self.axes_plot[0], self.axes_plot[1], linewidth=2.0)
def add_Y1(self):
    self.axes_plot[2] = self.x
    self.axes_plot[3] = self.y1
    self.axes.plot(self.axes_plot[2], self.axes_plot[3], linewidth=2.0)
    self.Canvas_plot2D.draw()
© www.soinside.com 2019 - 2024. All rights reserved.