Search code examples
pythonplotwxmpl

how to use time formatting in wxmpl (NOT FOR MATPLOTLIB)


I have a time list below for plotting:

time_list = [63158,63159,63200,63201,63202]

But i want like this in graphic:

time_list = [6:31:58,6:31:59,6:32:00,6:32:01,6:32:02]

And between 63159 and 63200, shouldn't be behave as if there is 40 numbers among them.

I can use this with matplotlib library, but for my purpose wxmpl is better than it.

So my question is how can i use time formatting with any plot library except matplotlib?

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time
from wxmplot import PlotPanel
import wx

x=[]       
class MyFrame1 ( wx.Frame ):   

    def __init__( self, parent, **kws ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 595,476 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL, **kws )     

        self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        bSizer2 = wx.BoxSizer( wx.VERTICAL )

        gSizer1 = wx.GridSizer( 0, 0, 0, 0 )

        ###############################
        self.plotpanel = PlotPanel(self)                                  
        ###############################

        self.toggleBtn4 = wx.ToggleButton( self, wx.ID_ANY, u"Plot", wx.DefaultPosition, wx.DefaultSize, 0 )
        gSizer1.Add( self.toggleBtn4, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL, 5 )

        bSizer2.Add( gSizer1, 1, wx.EXPAND, 5 )

        bSizer1.Add( bSizer2, 1, wx.EXPAND, 5 )
        bSizer3 = wx.BoxSizer( wx.VERTICAL )
        bSizer3.Add(self.plotpanel, 1, wx.ALL|wx.EXPAND, 0 )
        bSizer1.Add( bSizer3, 3, wx.EXPAND, 5 )
        self.SetSizer( bSizer1 )
        #bSizer1.Fit(self)

        self.Centre( wx.BOTH )

        # Connect Events     
        self.toggleBtn4.Bind( wx.EVT_TOGGLEBUTTON, self.plot )   

    def plot( self, event ):
        time_list = [63150,63151,63152,63153,63154,63155,63156,63157,63158,63159,63200,63201,63202,63203,63204,63205,63206,63207,63208,63209,63210]
        y = [100,101,105,124,119,134,89,147,98,75,52,150,143,156,99,13,54,68,100,85,87]
        obj = event.GetEventObject()
        isPressed = obj.GetValue()  
        if isPressed:
            for i in time_list:                                  
                hour = i//10000
                minute = (i//100)%100
                second = i%100
                time_new = hour*3600 + minute*60 + second                             
                x.append(time_new)                                                 

                print("i   : ",i)
                print("zaman : ",time_new)

                time4 = time.strftime('%H:%M:%S', time.gmtime(time_new))
                print("time  : ",time4,"\n")

            self.plotpanel.plot(x, y)

        else: 
            self.Destroy()


if __name__ == "__main__":
    app = wx.App(False)             
    f = MyFrame1(None)       
    f.Show(True)          
    app.MainLoop()

Solution

  • Currently, wxmplot.PlotPanel.plot() (and .oplot()) supports auto-converting of xdata into date-time values only when using plot(xdata, ydata, use_dates=True) and only with xdata as Unix timestamps.

    That is, use_dates=True does

    from datetime import datetime
    from matplotlib import dates
    
    ...
    def plot(xdata, ydata, ..., use_dates=False, ....):  
       ...
       if use_dates: 
           xdata = dates.date2num([datetime.fromtimestamp(i) for i in xdata])
    

    So, I think you will have to convert your custom encoding of time data into a more standard representation of times. I think you have two options:

    1. convert your time_list data into timestamps (seconds since epoch) and use use_dates=True.

    2. convert your time_list data into an instance of matplotlib.dates, possibly using datetime as an intermediate form and use use_dates=False.