gpt4 book ai didi

python - 将图 "caracteristics"从一个类复制到另一个类

转载 作者:太空宇宙 更新时间:2023-11-03 20:05:41 24 4
gpt4 key购买 nike

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

您有第一个图,是用“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 建立了一个接口(interface),并且我想从其中的其他文件绘制曲线。

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

最佳答案

您可能更愿意创建一个单独的类来输入不同的数据。

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()

关于python - 将图 "caracteristics"从一个类复制到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58981945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com