【发布时间】:2017-02-08 14:42:33
【问题描述】:
我按照 Benjamin Root 的“使用 Matplotlib 的交互式应用程序”一书中的教程对 matplotlib 图像轴的 format_coord() 方法进行了子类化。有用。我有一个 gui 应用程序,其中 main.py 导入数据指针并使用它来更改当我将鼠标移到图像上时显示的交互式数字。
from gui_stuff.datapointer import DataPointerLeftCart
然后通过调用来使用它:
self.ax_lhs = self.f_lhs.add_subplot(111,projection = 'sp_data_pointer')
DataPointerLeftCart 的精简代码在单独的文件中定义如下:
import matplotlib.projections as mproj
import matplotlib.transforms as mtransforms
from matplotlib.axes import Axes
import matplotlib.pyplot as plt
#Our own modules
from operations.configurations import configParser
class DataPointerLeftCart(Axes):
name = 'sp_data_pointer'
def format_coord(self, y, z):
configparams = configParser(ConfigFile=configfilename)
centreY = float(configparams['CENTRE_Y'])#units are pixels
scale = float(configparams['SCALE'])#nm^-1 per pixel
new_qy = (y-(centreY))*scale
return "Qy: %f (nm^-1)" % (new_qy)
mproj.projection_registry.register(DataPointerLeftPol)
configParser() 是一个函数,它读取文本文件并为我的程序创建一个包含各种重要配置编号的字典。最初这没有参数,并且 configParser 指定了文本文件的位置,但最近我修改了整个程序,以便您指定配置文件的本地副本。这要求我能够传递 configfilename 参数。但是,我对如何做到这一点感到困惑。 configfilename 必须来自 main.py 但这里我只将名称“sp_data_pointer”作为 add_subplot 的参数。
这让我感到困惑,因为我没有在任何地方(在我的代码中明显地)创建类的实例,并且大概“init”方法在我子类化的 Axes 内部得到了处理。有人可以解释一下原理和/或一个肮脏的解决方法来让我动起来吗(最好两者都有!)
【问题讨论】:
标签: python matplotlib instance subclass interactive