【问题标题】:Copy plot "caracteristics" from a class to another将绘图“特征”从一个类复制到另一个
【发布时间】:2019-11-21 18:58:02
【问题描述】:

我想知道是否可以将绘图特征从一个图形复制到另一个图形。举个例子:

您有第一个绘​​图,在“A”类的方法中制作,其中绘制了曲线,定义了 xlim 和网格:

class A:
   def __init__(self):
       self.plot()
   def plot(self):
       test1=[[1.11,1.12,1.13,1.14,1.12,1.13,1.14,1.15], [1,1,1,1, 5,5,5,5], [0,11,20,30,0,11,20,30]] 
       self.data=pd.DataFrame(test1).T
       ax = plt.gca()
       self.data.plot(ax=ax)

       ax.set_xlim(0, 40)

       n_x,n_y=11,8.5
       ax.set_aspect( ax.get_xlim()[1]/ax.get_ylim()[1] * n_y/n_x  ) 
       # Customize the major grid
       ax.grid(which='major', linestyle='-', linewidth='1.1', color='black')
       # Customize the minor grid
       ax.grid(which='minor', linestyle=':', linewidth='0.5', color='black')

那么你有另一个类“B”,在另一个文件上:

 class B:
   def __init__(self):
       self.plot()
   def plot(self):
       test2=[[2.55,6.55,0.33], [1.2,2.2,2.3]] 
       self.data=pd.DataFrame(test2).T
       self.data.plot()

如果我调用 A 类,我可以复制网格和 xlim 而不在 B 类中再次这样做吗?就像创建一个包含所有这些特征的变量?

我可以例如定义那些 xlim 和 grid 而不在 A 类中绘图而只在 B 类中绘图吗?事实上,B 类在一个 GUI 文件中,我在其中与 Tkinter 做了一个接口,我想从其中的另一个文件中绘制曲线。

希望我的回答有点清楚^^谢谢:)

【问题讨论】:

    标签: python python-3.x pandas matplotlib plot


    【解决方案1】:

    您可能更愿意创建一个接收不同数据的单一类。

    import matplotlib.pyplot as plt
    import pandas as pd
    
    class Base():
        def __init__(self, data):
            self.data = pd.DataFrame(test1).T
    
        def plot(self, ax=None):
            ax = ax or plt.gca()
            self.data.plot(ax=ax)
            ax.set_xlim(0, 10)
    
            n_x,n_y=11,8.5
            ax.set_aspect( ax.get_xlim()[1]/ax.get_ylim()[1] * n_y/n_x  )  
            ax.grid(which='major', linestyle='-', linewidth='1.1', color='black')
            ax.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
    
    
    test1=[[1.11,1.12,1.13,1.14,1.12,1.13,1.14,1.15], [1,1,1,1, 5,5,5,5], [0,11,20,30,0,11,20,30]] 
    test2=[[2.55,6.55,0.33], [1.2,2.2,2.3]] 
    
    b1 = Base(test1)
    b2 = Base(test2)
    b1.plot()
    b2.plot()
    plt.show()
    

    【讨论】:

    • 问题是第一个类在一个 GUI 文件中,我在其中与 Tkinter 进行了接口(由 B 类表示),并且我想合并在另一个文件中制作的曲线(A 类在哪里) .另外我想知道是否可以在绘图文件中定义 grid 和 xlim 并在 GUI 中调用它们
    • 不要将绘图与 GUI 界面混用。
    • 那么如何导入界面中的曲线以及界面中“绘图文件”中定义的所有特征?
    • 我想我不明白这个问题。
    猜你喜欢
    • 2010-11-15
    • 2018-10-20
    • 2021-07-09
    • 2012-03-06
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多