You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将X-Y图转换为X-Y-Y1图并实现多Y轴自动添加?

问题说明

现有一张X-Y图,需要在同一张图中添加Y1数据,将其转换为X-Y-Y1图,同时实现自动化添加多个Y轴的功能。尝试通过初始化空列表实现时,出现ValueError: Input could not be cast to an at-least-1D NumPy array报错。

初始报错代码

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.resizable(0,0)
        Main.geometry("900x550")
        Main.title("Main")

        # 创建透明背景的绘图对象
        transparent = (0, 0, 0, 0)
        plot_2D = plt.figure(figsize=(2,2), facecolor=transparent)

        # 创建坐标轴并关联到绘图对象
        self.axes = plot_2D.add_axes([0.1, 0.1, 0.8, 0.8])

        # 生成X轴数据
        dt = 1
        t = np.arange(0, 30, dt)
        self.x = t

        # 生成Y轴数据
        self.y = np.sin(2 * np.pi * 0.2 * t)

        # 生成Y1轴数据
        self.y1 = np.sin(2 * np.pi * 5 * t) 

        # 创建空列表
        self.axes_plot = [[None] for _ in range(4)]

        # 向列表中添加X和Y数据
        self.axes_plot[0] = self.x
        self.axes_plot[1] = self.y

        # 通过列表绘制图像(此处报错)
        self.axes.plot(self.axes_plot, linewidth=2.0) # <----- 报错位置
        # ValueError: Input could not be cast to an at-least-1D NumPy array

        # 在主窗口上绘制图像
        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()

        # 创建添加Y1轴的按钮
        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)

    # 添加Y1轴的尝试方法
    def add_Y1(self):

        # 向列表中添加Y1相关数据
        self.axes_plot[2] = self.x
        self.axes_plot[3] = self.y1

        # 重新绘制图像
        self.axes.plot(self.axes_plot, linewidth=2.0)
        self.Canvas_plot2D.draw()



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

修改后的代码(修复报错)

感谢László Hunyadi的建议,修改后的代码如下:

# 创建空列表
    self.axes_plot = [[None] for _ in range(4)]

    # 向列表中添加X和Y数据
    self.axes_plot[0] = self.x
    self.axes_plot[1] = self.y

    # 通过列表索引绘制图像
    self.axes.plot(self.axes_plot[0],self.axes_plot[1], linewidth=2.0) 

    # 在主窗口上绘制图像
    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()

    # 用于按钮命令的参数
    # 此处arg1返回'2',由于索引从0开始,可直接用于扩展列表
    arg1 = len(self.axes_plot)

    # 创建添加Y1轴的按钮
    extract_2d_button=Button(Main, text="Add Y1", width=10, heigh=2, command= lambda: self.add_Y1(arg1))
    extract_2d_button.place(anchor='nw',x=10, y=460)

# 通过扩展列表自动添加新Y轴的方法  
def add_Y1(self, a):

    # 向列表中添加Y1数据
    self.axes_plot.append(self.y1)

    # 重新绘制图像,X轴复用self.axes_plot[0]的数据
    self.axes.plot(self.axes_plot[0], self.axes_plot[a], linewidth=2.0)
    self.Canvas_plot2D.draw()

内容的提问来源于stack exchange,提问作者i33SoDA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 04:30:59