当前位置: 首页 > 面试题库 >

Matplotlib:使用图形对象初始化图形

薛泰
2023-03-14
问题内容

我正在为特定实验构建一类绘图工具。我目前有两种绘图方法,一种使用imshow()的静态绘图,一种也使用imshow()的“电影”格式。

这两种方法以及任何将来的方法都将获得与我可能编写的任何特定绘图方法相同的参数。使用plot类时,我在配置对象中具有所有这些参数。

我不想在每种绘图方法中都重写代码。我想初始化一个将设置以下args的对象(我认为是AxesImage):vmin,vmax,extent_dim,Xlocs,Xlabels,Ylocs,Ylabels。

然后,我只是将该对象传递给执行其他某些特定操作的各种方法。我不知道该怎么做…

import matplotlib.pyplot as plt

data = data_dict[type] # could be real part of a complex number, phase, or the mag...
v_min, v_max = self.get_data_type_scale(data_dict, Type)
freq = data_dict['freq']

# essentially sets the aspect of the plot since the x and y resolutions could be different   
extent_dim = self._get_extent(2)
# gets the labels for physical dimensions of the experiment
Xlocs,Xlabels,Ylocs,Ylabels = self._get_ticks(5,5,extent_dim)

# in the guts of a plot method, the basic idea is the call below.

plt.imshow(data[0,:,:],cmap='jet',vmin=v_min,...
vmax=v_max,origin='lower', extent = extent_dim)

plt.title('Type:  %s  Freq: %.3e Hz' %(Type,data_dict['freq'][0]) )
plt.xticks(Xlocs, Xlabels)
plt.yticks(Ylocs,Ylabels)

问题答案:

您需要先了解一些架构matplotlib(请参阅此处,了解创始人和当前主要开发人员的长篇文章)。在backend处理渲染和与硬件对话的层的底部。在该层的顶部artists,其知道如何绘制通过告诉他们的自我backend对象做什么。在该层的顶层是模仿的pyplot状态机接口MATLAB

您在图中看到的所有内容在内部都以表示,Artist并且艺术家可以包含其他艺术家。例如,Axes对象跟踪子对象,即Artists刺,children,标签,线条或图像等轴,而Axes对象则是对象的子Figure对象。当您(通过fig.canvas.draw())告诉人物自己绘画时,所有的子代画家都是递归绘制的。

这种设计的一个缺点是,给定的an实例Artist可以恰好在一个图形中(并且很难在图形之间移动它们),因此您无法创建AxesImage对象然后继续重复使用它。

这种设计也将Artists已知信息分开。
Axes对象了解tick的位置,标签和显示范围(通过了解Axis对象来实现,但这会使杂草更多)。像vminvmax封装在Normalize(doc)对象中,以进行AxesImage跟踪。这意味着您将需要分开处理列表中所有内容的方式。

我建议在这里使用工厂式模式或咖喱式模式

类工厂:

def set_up_axes(some, arguements):
    '''
    Factory to make configured axes (
    '''
    fig, ax = plt.subplots(1, 1) # or what ever layout you want
    ax.set_*(...)
    return fig, ax


my_norm = matplotlib.colors.Normalize(vmin, mmax) # or write a factory to do fancier stuff
fig, ax = set_up_axes(...)
ax.imshow(..., norm=my_norm)
fig2, ax2 = set_up_axes(...)
ax2.imshow(..., norm=mynorm)

您可以包装一整套kwarg,以方便地按如下方式重复使用它们:

my_imshow_args = {'extent':[...],
                  'interpolation':'nearest',
                  'norm': my_norm,
                   ...}

ax2.imshow(..., **my_imshow_args)

咖喱状:

def my_imshow(im, ax=None, *args, **kwargs):
    if ax is None:
        ax = plt.gca()
    # do all of your axes set up
    ax.set_xlim(..)

    # set default vmin and vmax
    # you can drop some of these conditionals if you don't want to be
    # able to explicitly override the defaults
    if 'norm' not in kwargs:
        vmin = kwargs.pop('vmin', None)
        vmax = kwargs.pop('vmax', None)
        if vmin is None:
            vmin = default_vmin # or what ever
        if vmax is None:
            vmax = default_vmax
        my_norm = matplotlib.colors.Normalize(vmin, mmax)
        kwargs['norm'] = norm

    # add a similar block for `extent` 
    # or any other kwargs you want to change the default of

    ax.figure.canvas.draw() # if you want to force a re-draw
    return ax.imshow(im, *args, **kwargs)

如果您想变得聪明,可以plt.imshow使用您的版本进行猴子补丁

plt.imshow = my_imshow

还有一个rcParams接口,它允许您以matplotlib全局方式更改许多位的默认值。

还有另一种方法(通过partial)来完成此任务



 类似资料:
  • 我正在为一个特定的实验构建一类绘图工具。我目前有两种绘图方法,一种是使用imshow()的静态绘图,另一种是使用imshow()的“电影”格式。 无论是方法还是任何未来的方法,都要获取与我可能编写的任何特定绘图方法相同的参数。在使用情节类时,我在一个配置对象中拥有所有这些参数。 我不想在每个plot方法中重写代码。我想初始化一个对象(我想是AxeImage),它将设置以下参数:vmin、vmax、

  • 我在使用Mockito进行单元测试初始化对象时遇到了一些困难 这是我的测试代码 要测试的代码 RecTangleService、CircleService和SquareService用注释我尝试了很多选项,最终得出结论。我没有得到我错在哪里。我试着在网上搜索了很多地方,但找不到任何帮助。

  • 图形对象是用以显示图形和用户界面元素的基本元素。下表列出了各种图形对象。 对象 描述 Root 对计算机屏幕最高级的对象 Figure 用来显示图形和用户界面的窗口 Axes 在窗口中显示图形的轴 Uicontrol 用户界面控制。执行一个对用户交互作用的函数。 Uimenu 用户定义窗口菜单 Uicontextmenu 右键单击对象时弹出的菜单 Image 二维像素基础图 Light 影响斑点和

  • 我预计Spring会通过将属性“property.key”设置为“property_value”来初始化StorageConfiguration对象。 但是,我得到以下异常 org.springframework.beans.factory.beanCreationException:创建类路径资源[applicationContext.xml]中定义的名为“storage”的bean时出错:在设

  • 所以我做了一个在IDE中运行良好的小JavaFX项目。但是当导出到可运行的jar中时,双击不运行。