# Plot a Dictionary, time and value

Hi,

Wondering if anyone has done something similar and could point me in the
right direction.

I have a dictionary like this:

Dict{'00:00:00':'23', '00:01:00':'29', '00:02:00':'13', '00:03:00':'78',
'00:04:00':'45', >....> '23:59:00':54}

So as you can see there is 24 hours worth of minutes, with a value attached
to each minute.

Firstly, just to note the Dictionary "Dict" is not actually in order as
above, it is all jumbled up.

However is it possible to plot a dictionary using MatPlotLib, and using the
time along the x-axis and values up the y?

···

--
View this message in context: http://www.nabble.com/Plot-a-Dictionary%2C-time-and-value-tp18734294p18734294.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

You will have to extract the x and y values, and convert them from
strings to values matplotlib can understand (for example dates and
floating point numbers). Eg

In [30]: d = {'00:00:00':'23', '00:01:00':'29', '00:02:00':'13',
'00:03:00':'78',
'00:04:00':'45', '23:59:00':54}

In [32]: from dateutil.parser import parse

In [33]: items = [(parse(date), float(val)) for date, val in d.items()]

In [34]: items.sort()

In [35]: items
Out[35]:
[(datetime.datetime(2008, 7, 30, 0, 0), 23.0),
(datetime.datetime(2008, 7, 30, 0, 1), 29.0),
(datetime.datetime(2008, 7, 30, 0, 2), 13.0),
(datetime.datetime(2008, 7, 30, 0, 3), 78.0),
(datetime.datetime(2008, 7, 30, 0, 4), 45.0),
(datetime.datetime(2008, 7, 30, 23, 59), 54.0)]

In [36]: dates, values = zip(*items)

In [37]: plot(dates, values)
Out[37]: [<matplotlib.lines.Line2D object at 0xb45a5ec>]

···

On Wed, Jul 30, 2008 at 9:17 AM, stuartornum <stuart@...2110...> wrote:

Hi,

Wondering if anyone has done something similar and could point me in the
right direction.

I have a dictionary like this:

Dict{'00:00:00':'23', '00:01:00':'29', '00:02:00':'13', '00:03:00':'78',
'00:04:00':'45', >....> '23:59:00':54}

So as you can see there is 24 hours worth of minutes, with a value attached
to each minute.

Firstly, just to note the Dictionary "Dict" is not actually in order as
above, it is all jumbled up.

However is it possible to plot a dictionary using MatPlotLib, and using the
time along the x-axis and values up the y?

hmm, reading the initial email, this is not what I understood the idea would be. So let me the following : I have a dictionnary with the 7 days of week as keys (strings) and a value attached to it. I would like to plot the days of the week in x and the corresponding values in y. It amounts to a histogram of 7 bins, and to correctly labeling the ticks with the keys instead of the integer 0...6. So it becomes kind of a bar chart. See p.40 of ftp://root.cern.ch/root/doc/3Histograms.pdf for an illustration.

I would imagine that requesting to plot a dictionary would naturally mean this kind of result, but I may overlook possible ambiguities.
Anyway, that is what I understood the initial question was.

Johann

John Hunter wrote:

···

On Wed, Jul 30, 2008 at 9:17 AM, stuartornum <stuart@...2110...> wrote:

Hi,

Wondering if anyone has done something similar and could point me in the
right direction.

I have a dictionary like this:

Dict{'00:00:00':'23', '00:01:00':'29', '00:02:00':'13', '00:03:00':'78',
'00:04:00':'45', >....> '23:59:00':54}

So as you can see there is 24 hours worth of minutes, with a value attached
to each minute.

Firstly, just to note the Dictionary "Dict" is not actually in order as
above, it is all jumbled up.

However is it possible to plot a dictionary using MatPlotLib, and using the
time along the x-axis and values up the y?

You will have to extract the x and y values, and convert them from
strings to values matplotlib can understand (for example dates and
floating point numbers). Eg

In [30]: d = {'00:00:00':'23', '00:01:00':'29', '00:02:00':'13',
'00:03:00':'78',
'00:04:00':'45', '23:59:00':54}

In [32]: from dateutil.parser import parse

In [33]: items = [(parse(date), float(val)) for date, val in d.items()]

In [34]: items.sort()

In [35]: items
Out[35]:
[(datetime.datetime(2008, 7, 30, 0, 0), 23.0),
(datetime.datetime(2008, 7, 30, 0, 1), 29.0),
(datetime.datetime(2008, 7, 30, 0, 2), 13.0),
(datetime.datetime(2008, 7, 30, 0, 3), 78.0),
(datetime.datetime(2008, 7, 30, 0, 4), 45.0),
(datetime.datetime(2008, 7, 30, 23, 59), 54.0)]

In [36]: dates, values = zip(*items)

In [37]: plot(dates, values)
Out[37]: [<matplotlib.lines.Line2D object at 0xb45a5ec>]

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Thank you John,

Just what I was looking for.

John Hunter-4 wrote:

···

On Wed, Jul 30, 2008 at 9:17 AM, stuartornum <stuart@...2110...> wrote:

Hi,

Wondering if anyone has done something similar and could point me in the
right direction.

I have a dictionary like this:

Dict{'00:00:00':'23', '00:01:00':'29', '00:02:00':'13', '00:03:00':'78',
'00:04:00':'45', >....> '23:59:00':54}

So as you can see there is 24 hours worth of minutes, with a value
attached
to each minute.

Firstly, just to note the Dictionary "Dict" is not actually in order as
above, it is all jumbled up.

However is it possible to plot a dictionary using MatPlotLib, and using
the
time along the x-axis and values up the y?

You will have to extract the x and y values, and convert them from
strings to values matplotlib can understand (for example dates and
floating point numbers). Eg

In [30]: d = {'00:00:00':'23', '00:01:00':'29', '00:02:00':'13',
'00:03:00':'78',
'00:04:00':'45', '23:59:00':54}

In [32]: from dateutil.parser import parse

In [33]: items = [(parse(date), float(val)) for date, val in d.items()]

In [34]: items.sort()

In [35]: items
Out[35]:
[(datetime.datetime(2008, 7, 30, 0, 0), 23.0),
(datetime.datetime(2008, 7, 30, 0, 1), 29.0),
(datetime.datetime(2008, 7, 30, 0, 2), 13.0),
(datetime.datetime(2008, 7, 30, 0, 3), 78.0),
(datetime.datetime(2008, 7, 30, 0, 4), 45.0),
(datetime.datetime(2008, 7, 30, 23, 59), 54.0)]

In [36]: dates, values = zip(*items)

In [37]: plot(dates, values)
Out[37]: [<matplotlib.lines.Line2D object at 0xb45a5ec>]

-------------------------------------------------------------------------
challenge
Build the coolest Linux based applications with Moblin SDK & win great
prizes
Grand prize is a trip for two to an Open Source event anywhere in the
world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
View this message in context: http://www.nabble.com/Plot-a-Dictionary%2C-time-and-value-tp18734294p18750474.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hello,