matplotlib + wxpython不显示x-ax时间字符串数据

时间:2017-05-25 06:33:04

标签: python matplotlib wxpython

我是matplotlib的新手,我用它来显示.csv文件的数据,代码在下面,当我的鼠标移动触发时,xdata显示在状态栏上(链接代码功能如下: “onMotion”)不是我想要的(显示正确的时间字符串为HH:MM:来自原始.csv文件的SS),我想这是一个浮点数但是当我把它转换为时间时,它仍然不是我所期望的。我该怎么做才能做到对?任何建议都会提前得到很多赞赏。

CSV file data is below:
Time    CPU MEMEMORY    FLOW
21:41:45    7   2.065984885 0
21:41:49    24  2.143804486 207.1622516
21:41:53    18  2.099176758 254.0634666
21:41:57    16  2.148127797 546.959479
21:42:01    25  2.120096005 837.3973892
21:42:05    14  2.123164162 865.6367548
21:42:09    19  2.126511241 894.4122738
21:42:13    16  2.090111751 924.6048394
21:42:17    16  2.088717134 953.8266646
21:42:21    18  2.090948521 985.3669382

正如您所见,此文件中的时间格式为HH:MM:SS,但matplotlib显示如下图所示。 this

import matplotlib.pyplot as plt
import pandas as pd
import wx
class CanvasFrame(wx.Frame): 
    orgData= None
    def __init__(self):

        self.pro = None
        wx.Frame.__init__(self,None,-1, 'App Detection',size=(800,350))  
        self.orgData =pd.read_csv('data.csv')
        self.orgData["Time"] = pd.to_datetime(self.orgData["Time"])
        self.SetBackgroundColour("WHITE")  

        self.figure = plt.figure() 
        self.axes = self.figure.add_subplot(111)  

        self.CPU, = self.axes.plot(self.orgData["Time"], self.orgData["CPU"])
        self.MEMEMORY, = self.axes.plot(self.orgData["Time"], self.orgData["MEMEMORY"], "m")

        plt.xlabel("Time")
        plt.ylabel("Usage(%)")
        plt.title("Usage of App")

        self.axes2 = self.axes.twinx()
        self.FLOW, = self.axes2.plot(self.orgData["Time"], self.orgData["FLOW"], "r")
        self.axes2.set_ylabel("FLOW(kb)")

        self.ins = (self.CPU,)+(self.MEMEMORY,)+(self.FLOW,)
        self.labs = [l.get_label() for l in self.ins]
        self.axes.legend(self.ins, self.labs, loc=2)

        self.canvas = FigureCanvas(self, -1, self.figure)

        self.statusbar = self.CreateStatusBar()  
        self.mouseMoveID = self.canvas.mpl_connect('motion_notify_event',self.onMotion)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.btnStart = wx.Button(self, -1, "Start")
        self.Bind(wx.EVT_BUTTON, self.getData, self.btnStart)

        self.btnGen = wx.Button(self, -1, "Gen Chart")
        self.Bind(wx.EVT_BUTTON, self.genFigure, self.btnGen)

        self.btnSave = wx.Button(self, -1, "Save")
        self.Bind(wx.EVT_BUTTON, self.savePic, self.btnSave)

        self.tip = wx.StaticText(self, -1, u"Tip: Tip string!")
        self.tip.SetForegroundColour("red")
        self.sizerH = wx.GridSizer(1,0)
        self.sizerH.Add(self.btnStart,10, wx.ALL, 10)
        self.sizerH.Add(self.btnGen,10, wx.ALL, 10)
        self.sizerH.Add(self.btnSave,10, wx.ALL, 10)   
        self.sizer.Add(self.sizerH)
        self.sizer.Add(self.tip)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)  
        self.SetSizer(self.sizer)  
        self.Fit()

    def onMotion(self, evt):
        x = evt.x
        y = evt.y
        inaxes = evt.inaxes
        xdata = evt.xdata
        ydata = evt.ydata
        self.statusbar.SetStatusText("%s, %s, %s, %s, %s" % (
        x, y, inaxes, xdata, ydata))

1 个答案:

答案 0 :(得分:0)

要格式化日期或时间,您可以使用matplotlib.dates.DateFormatter,如图所示在this example。使用相应的时间调用此dateformatter将生成所需的格式化字符串。

dateformatter = mdates.DateFormatter('%H:%M:%S')
dateformatter(xdata)

显示格式化时间的问题实际上是GUI实现的独立问题。所以我把它留下来,只提供一个解决方案,显示轴内的格式化时间。从问题中可以清楚地看到如何将格式化的字符串提供给GUI的状态栏。

u = u"""Time    CPU MEMEMORY    FLOW
21:41:45    7   2.065984885 0
21:41:49    24  2.143804486 207.1622516
21:41:53    18  2.099176758 254.0634666
21:41:57    16  2.148127797 546.959479
21:42:01    25  2.120096005 837.3973892
21:42:05    14  2.123164162 865.6367548
21:42:09    19  2.126511241 894.4122738
21:42:13    16  2.090111751 924.6048394
21:42:17    16  2.088717134 953.8266646
21:42:21    18  2.090948521 985.3669382"""

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import io

df = pd.read_csv(io.StringIO(u), delim_whitespace=True)

df["Time"] = pd.to_datetime(df["Time"])

fig, ax = plt.subplots()

ax.plot(df["Time"], df["CPU"])
ax.plot(df["Time"], df["MEMEMORY"])

text = ax.text(0.02,0.98, "", transform=ax.transAxes, va="top")
dateformatter = mdates.DateFormatter('%H:%M:%S')

def onmousemove(evt):
    if evt.inaxes:
        xdata = evt.xdata
        xtime =  dateformatter(xdata)
        string = "%s" % (xtime)
        text.set_text(string)
        fig.canvas.draw_idle()

cid = fig.canvas.mpl_connect("motion_notify_event",onmousemove)

plt.show()

enter image description here