Discussion:
[Matplotlib-users] Using dates in a simple plot with wxPython doesn't work
by way of c.buhtz@posteo.jp
2015-07-28 17:31:49 UTC
Permalink
I try to use a dates on the x-aches. With pure Python3 code it works
fine. BUt when I try to use this inside wxPython application on a
FigureCanvas it doesn't work. And I don't understand the difference
here.

This is the error
[err]
plot.plot([datetime.date(2015, 1, 7),
TypeError: descriptor 'date' requires a 'datetime.datetime' object but
received a 'int'
[/err]

This is the fine working pure Python3 code.
[code]
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import datetime

plt.plot([datetime.date(2015, 1, 7),
datetime.date(2015, 2, 5),
datetime.date(2015, 6, 2)],
[70.3, 60.1, 68.8])
plt.show()
[/code]

This is the piece of wxPython code that cause the error
[code]
# -*- coding: utf-8 -*-
import matplotlib.pyplot as pyplot
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as
FigureCanvas
from datetime import datetime

#...

class StatisticTab(wx.Panel):
def __init__(self, parent):
super(StatisticTab, self).__init__(parent)

panel = wx.Panel(self)

fig = pyplot.figure()
plot = self.fig.add_subplot(1,1,1)
plot.plot([datetime.date(2015, 1, 7),
datetime.date(2015, 2, 5),
datetime.date(2015, 6, 2)],
[70.3, 60.1, 68.8])

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

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(panel)
self.SetSizer(sizer)
[/code]

------------------------------------------------------------------------------
Brendan Barnwell
2015-07-28 17:54:09 UTC
Permalink
Post by by way of ***@posteo.jp
I try to use a dates on the x-aches. With pure Python3 code it works
fine. BUt when I try to use this inside wxPython application on a
FigureCanvas it doesn't work. And I don't understand the difference
here.
This is the error
[err]
plot.plot([datetime.date(2015, 1, 7),
TypeError: descriptor 'date' requires a 'datetime.datetime' object but
received a 'int'
[/err]
This is the fine working pure Python3 code.
<snip>
Post by by way of ***@posteo.jp
import datetime
<snip>
Post by by way of ***@posteo.jp
This is the piece of wxPython code that cause the error
<snip>
Post by by way of ***@posteo.jp
from datetime import datetime
There is the problem. The error has nothing to do with matplotlib. In
one case you did "import datetime" and imported the datetime library.
In the other you did "from datetime import datetime", thus importing the
datetime type from that library. If your first version is working,
change the second version to use the same import.
--
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is no
path, and leave a trail."
--author unknown

------------------------------------------------------------------------------
Loading...