Search code examples
pythonpython-2.7matplotlibwxpythonwxwidgets

plotting date series with wxpython and matplotlib - x and y must have same first dimension


I am trying to plot some date series data. As soon as I add the dates I get an error. It works fine if I replace them with numbers. What am I doing wrong?

My code:

import numpy
import matplotlib
matplotlib.use('WXAgg')

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure

import wx

class CanvasPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)
        self.Fit()

    def draw(self):
        t = ['1/1/2013','1/2/2013','1/3/2013','1/4/2013','1/5/2013','1/6/2013',
             '1/7/2013','1/8/2013','1/9/2013']
        s = [100,121,89,111,343,211,151,232,122,343]
        self.axes.plot(t, s)
        self.tittle1 =  wx.StaticText(self, label="My Label")

if __name__ == "__main__":
    app = wx.PySimpleApp()
    fr = wx.Frame(None, title='My Data')
    panel = CanvasPanel(fr)
    panel.draw()
    fr.Show()
    app.MainLoop()

Error:

ValueError: x and y must have same first dimension

Solution

  • In [1]: t = ['1/1/2013','1/2/2013','1/3/2013','1/4/2013','1/5/2013','1/6/2013','1/7/2013','1/8/2013','1/9/2013']
    
    In [2]: s = [100,121,89,111,343,211,151,232,122,343]
    
    In [3]: len(t)
    Out[3]: 9
    
    In [4]: len(s)
    Out[4]: 10