0

我一直使用此代码作为模型来修改应用程序,以支持覆盖在同一轴上的超过 8 个单独的图和两侧的不同比例。

到目前为止,我遇到了几个问题。

  1. 一些 Y 标签在左边,一些在右边。2 或 3 在两边(不是全部)重复(错误)。它似乎在右侧覆盖重复导致乱码。一个比例具有绘制的数据的数量。

  2. 当应用程序停止时,黑色窗口(DOS 提示窗口)不会消失。我有一个理论,即创建了两个“画布”块,一个通过调用创建实例,一个调用创建画布,另一个创建绘图区域(而不是画布)。调用如下:

设置 1

myLOG_Fig = m_plt.figure(figsize=(3.0, 3.0), dpi=100)
self.myLOG_PlotCanvas = FigCanvas( panel, -1, myLOG_Fig )

设置 2

self.myLOG_HostAxes = host_subplot(111, axes_class=AA.Axes)

需要设置 1 来self.myLOG_PlotCanvasSizer放置例程呈现,该例程将图像(绘图区域画布)应用到面板(四个面板之一)上。在我开始画布上的其他情节(2 到 8)之前,这套作品效果很好。

我找到了使用 mpl Toolkit 和 AA 来允许共享 X 轴并呈现超过 2 个刻度的解决方案(第 2 组)。通过更改代码以允许设置 2 是我注意到问题的地方。这些图被正确地接受以缩放和着色。侧面比例几乎看起来不错(右侧重复出现例外)并且我无法更改 X 轴标签和 Y 轴标签的字体大小。我可以使用设置 1 控制颜色和字体大小。设置 2 对更改字符属性没有响应。还有一些其他代码必须使用 set 2 所需的代码(但我没有在这里列出)。我个人认为这两个画布绘图区域并不相同,这也是应用程序不死的原因之一(黑色 DOS 窗口徘徊)。

是否有更新版本的 matplotlib 和 mpl_toolkit 代码将例程连接到一个画布绘图区域?是否有一些代码可以满足我的要求(使用 sizer 等)来生成一个带有选项卡和每个选项卡上的绘图区域的应用程序?

4

1 回答 1

0

从此代码生成的绘图

以下是我对我的问题的解决方案(我在 2013 年发布)。在寻找答案和解决方案后,我找到了几个代码片段并将它们拼凑在一起,使它们作为一个“团队”工作......省略了一些设置代码以保持列出的代码简短。每个 y 轴的数组是省略的部分之一。

代码片段如下:

class SubFrame_L(wx.Frame): """ SubFrame_L (2169 Lines) 子框架类,用于创建一个框架,其中包括用于绘制图像的图形和画布。
包含以下方法:

        BtnPlot      - button to invoke select a file to plot
        BtnClear     - button to clear canvas image
        BtnClose     - button to close instance of SubFrame_L
        BtnWordDoc   - button to Export image to Word Document file
        BtnPrint     - button to Export image to Word Document file & Print
        BtnClipboard - button to Export image to Clipboard

        DoPlot_SLIK_File   - select a file to plot, then transfers to canvas
        myLOG_DrawPlot     - processes file to plot data arrays.
        RePaintL_Canvas    - re-draws canvas after a canvas "clear" 
        Display_SLIK_Parms - displays Parameters read from file
        BtnClose_EVT       - event handler to clean up and close instance
        DblClick_EVT       - 
"""

    intDPI    = 100
    #
    fltFigX = 12.0
    fltFigY =  4.28

    self.myLOG_Fig = plt.figure(num=None, figsize=( fltFigX, fltFigY ), dpi=intDPI)

    self.myLOG_Fig.subplots_adjust(right=0.85)

    # New version of axes_grid1 with axisartist is recommended
    # host_subplot returns ax which is formed using plt.
    #
    # uses matplotlib.pyplot as plt
    #                               111 = rows,cols,subplot #
    self.x_AA_Axes_L = host_subplot(111, axes_class=AA.Axes)
    #
    self.x_AA_Axes_L.axis["top"].set_visible( True )
    self.x_AA_Axes_L.set_axis_bgcolor( 'white' )    
    self.x_AA_Axes_L.set_autoscale_on( False   )      # <<< ONLY False works

    # *** ++++++++++++++++++++++++++++++++++ Set X-AXIS for all Y-Scales
    self.x_AA_Axes_L.set_xlabel("Time (min)",  fontsize=8, color='red')


    # _________________________Offsets___________________________________
    # Y Label Axis offset from X-Axis
    right_Offset1 = 0                          # Temperature
    right_Offset2  =  right_Offset1 + 50       # Accelerometer
    right_Offset3  =  right_Offset2 + 50       # 

    left_offset1   = 0                         # Motor/Batt Voltage
    left_offset2   = left_offset1 - 50         # Motor Amps
    left_offset3   = left_offset2 - 20         # TBD
    # _________________________Offsets___________________________________


    # *** INIT *** SLIKPAK ***
    # +++++++++++++++++++++++++++++ Battery Voltage  (green) [left @ 0)  #-50]
    self.x_AA_Axes_L.set_ylabel("Battery Voltage", fontsize=8, color='green')
    self.x_AA_Axes_L.set_ylim(ymin=0.0,ymax=40.0)

    # +++++++++++++++++++++++++++++ Motor Current     (red) [left @ 0]
    self.axes_1_L = self.x_AA_Axes_L.twinx()    # Use subplot Base axis

    self.newFixedAxis_1_L = self.axes_1_L.get_grid_helper().new_fixed_axis


    self.axes_1_L.axis["left"] = self.newFixedAxis_1_L(loc="left",
                                        axes=self.axes_1_L,
                                        offset=(left_offset2, 0))

    # axis is shared by Batt and Mot - left is displayed, right is off
    self.axes_1_L.axis["right"].set_visible( False )

    self.axes_1_L.set_ylabel("force (g)/Motor Current (amps)", fontsize=8, color='red')
    self.axes_1_L.set_ylim( ymin=0.0, ymax=10.0 )

    # +++++++++++++++++++++++++++++ Temperature  (blue)
    self.axes_2_L = self.x_AA_Axes_L.twinx()

    self.newFixedAxis_2_L = self.axes_2_L.get_grid_helper().new_fixed_axis

    self.axes_2_L.axis["right"] = self.newFixedAxis_2_L(loc="right",
                                        axes=self.axes_2_L,
                                        offset=(right_Offset1, 0))

    # right is displayed, left is off
    self.axes_2_L.set_ylabel("Temperature", fontsize=8, color='blue')
    self.axes_2_L.set_ylim(ymin=0.0,ymax=300.0) #, size=8) #,fontsize=8, color='green' )



    # +++++++++++++++++++++++++++++ Accelerometer
    self.axes_3_L = self.x_AA_Axes_L.twinx()    # Use subplot Base axis

    self.newFixedAxis_3 = self.axes_3_L.get_grid_helper().new_fixed_axis

    self.axes_3_L.axis["right"] = self.newFixedAxis_3(loc="right",
                                        axes=self.axes_3_L,
                                        offset=(right_Offset2, 0))

    self.axes_3_L.set_ylabel("force [X(g),Y(o),Z(m)]", fontsize=8, color='green')
    self.axes_3_L.set_ylim(ymin=0.0,ymax=10.0)


    self.myLOG_FigCanvas = FigCanvas( self, -1, self.myLOG_Fig )  

    # *** Add special action buttons - pan, zoom, etc
    #
    self.NavToolBar_L = NavigationToolbar( self.myLOG_FigCanvas )
    #
    self.NavToolBar_L.Realize()

    # Start Main Box (Verticle Box)
    self.vMainBoxSizer1 = wx.BoxSizer(wx.VERTICAL)
    # *** (v1) First, Add FigCanvas to Vertical Box
    self.vMainBoxSizer1.Add( self.myLOG_FigCanvas, proportion=1, border=5, \
                                    flag=wx.ALL | wx.EXPAND)

    # *** Start Horizontal Box
    self.hBoxSizer = wx.BoxSizer(wx.HORIZONTAL)
    # *** (h1) Add Navigation Tool along with buttons to hBoxSizer (horizontal Box)
    self.hBoxSizer.Add( self.NavToolBar_L, proportion=0, border=2, flag=wx.ALL)

    self.btnPlot  = wx.Button( self, wx.NewId(), "Plot"  )
    self.btnClear = wx.Button( self, wx.NewId(), "Clear" )
    self.btnClose = wx.Button( self, wx.NewId(), "Close" )

    self.btnWordDoc   = wx.Button( self, wx.NewId(), "WordDoc" )
    self.btnPrint     = wx.Button( self, wx.NewId(), "Print" )
    self.btnClipboard = wx.Button( self, wx.NewId(), "Clipboard" )

    # *** (h2) Now, Add buttons to hBoxSizer (horizontal Box)
    self.hBoxSizer.Add( self.btnPlot,      proportion=0, border=2, flag=wx.ALL)
    self.hBoxSizer.Add( self.btnClear,     proportion=0, border=2, flag=wx.ALL)
    self.hBoxSizer.Add( self.btnClose,     proportion=0, border=2, flag=wx.ALL)
    self.hBoxSizer.Add( self.btnWordDoc,   proportion=0, border=2, flag=wx.ALL)
    self.hBoxSizer.Add( self.btnPrint,     proportion=0, border=2, flag=wx.ALL)
    self.hBoxSizer.Add( self.btnClipboard, proportion=0, border=2, flag=wx.ALL)

    # *** (v2) Add self.hBoxSizer to Verticla Box
    self.vMainBoxSizer1.Add( self.hBoxSizer )
    # *** Finalize self.vMainBoxSizer1
    self.SetSizer( self.vMainBoxSizer1 )

    # **** BIND Buttons to Handlers
    # This Canvas supports double-click to open another canvas window
    self.myLOG_FigCanvas.Bind( wx.EVT_LEFT_DCLICK, self.DblClick_L_EVT )

    self.btnPlot.Bind(  wx.EVT_BUTTON, self.BtnPlot_L_EVT  )
    self.btnPlot.Enable( True )
    self.btnClear.Bind( wx.EVT_BUTTON, self.BtnClearPlot_L_EVT )
    self.btnClear.Enable( False )
    # *** connect to "Close Button"
    self.btnClose.Bind( wx.EVT_BUTTON, self.BtnClose_L_EVT )
    # *** Also connect to "EXIT Button" Strip
    self.Bind(wx.EVT_CLOSE, self.BtnClose_L_EVT)
    self.btnWordDoc.Bind(  wx.EVT_BUTTON, self.BtnWordDoc_L_EVT )
    self.btnWordDoc.Enable( False )
    self.btnPrint.Bind( wx.EVT_BUTTON, self.BtnPrint_L_EVT )
    self.btnPrint.Enable( False )
    self.btnClipboard.Bind( wx.EVT_BUTTON, self.BtnClipboard_L_EVT )
    self.btnClipboard.Enable( False )

    self.Layout()

代码结束。绘图示例软件代码的开始:

    try:
        # *** Battery Voltage (30 Volts) (green)
        # Number of Time entries must match number of VBatt entries
        self.x_AA_Axes_L.plot( self.fltArray_Time_L,
                                self.fltArray_VBatt_L, 
                linewidth=1, color=('green'))[0]       # [0] required !!!
    except:
        pass
    #
    try:
        # *** Phase               (1-4) [0-10](black)
        # Number of Time entries must match number of Phase entries
        self.axes_1_L.plot( self.fltArray_Time_L,
                            self.fltArray_Phase_L, linewidth=1,
                            color=('black'))[0]            # [0] required !!!
    except:
        pass
    #
    try:
        # *** BrushLess Motor Amps (3 Amps) [0-10](red)
        # Number of Time entries must match number of BrLsAmps entries
        self.axes_1_L.plot( self.fltArray_Time_L,
                            self.fltArray_BrLsAmps_L, linewidth=1,
                            color=('red'))[0]            # [0] required !!!
    except:
        pass
    #
    #
    try:
        # *** Temperature (72 degrees) (blue)
        # Number of Time entries must match number of Temp entries
        self.axes_2_L.plot( self.fltArray_Time_L,
                            self.fltArray_Temp_L, 
                linewidth=1, color=('blue'))[0]       # [0] required !!!
    except:
        pass
    #
    #
    #
    try:
        # *** Gx  Accelerometer    (g force)(0-10) (orange)      <<<<<<<<<<<<<<<< ORANGE
        #
        self.axes_3_L.plot( self.fltArray_Time_L,
                            self.fltArray_AccX_L, linewidth=1,
                            color=('orange'))[0]          # [0] required !!!
    except:
        pass
    #
    try:
        # *** Gy  Accelerometer    (g force)(0-10) (green)
        #
        self.axes_3_L.plot( self.fltArray_Time_L,
                            self.fltArray_AccY_L, linewidth=1,
                            color=('green'))[0]          # [0] required !!!
    except:
        pass
    #
    #
    try:
        # *** Gz  Accelerometer    (g force)(0-10) (magenta)
        #
        self.axes_3_L.plot( self.fltArray_Time_L,
                            self.fltArray_AccZ_L, linewidth=1,
                            color=('magenta'))[0]          # [0] required !!!
    except:
        pass

代码结束

画布窗口的有趣特性是它支持双击,这会导致出现一个新的(空白)窗口。部分代码因空间而被省略。

选择包含 4 到 7 列数据的文件。每列都呈现给一个数组[](每个都有不同的名称)。对插入到数组中的项目的确切数量进行计数。array_of_count[] 将匹配数据数组中的每个条目。并将指示每个 x 轴步长的时间步长(在这种情况下为 0.0167)。已为感兴趣的人上传了一个绘图显示示例。

于 2014-10-07T21:30:01.913 回答