0

I'm trying to plot rather simple set of data stored in dictionary. Key is the date, when value is.. value. However x axis acts as normal set of values, instead of dates. They are plotted in the order, they are saved in the dictionary (not chronologically).There is as well no real scale on the x axis, so it's not plotting empty space on dates there is no data for.

On attached code there is only small part of the real dictionary, but it's enough to show the problem:

from matplotlib import pyplot as plt

dictionary = {'2015-11-15': 318, '2015-11-16': 3, '2015-11-18': 147, '2015-11-20': 38, 
'2015-11-22': 128,'2015-11-23': 37, '2015-11-24': 5, '2015-11-25': 3, '2015-11-26': 323, 
'2015-11-27': 478,'2015-11-28': 49, '2015-11-29': 9,'2015-12-11': 172, '2015-12-13': 219, 
'2015-12-16': 1,'2015-12-17': 10, '2015-12-18': 25, '2015-12-19': 3, '2015-12-2': 147, 
'2015-12-21': 133}

x = list(dictionary.keys())
y = list(dictionary.values())

plt.plot_date(x,y, xdate=True)
plt.xticks(x, rotation='vertical')
plt.show()

Any idea how to edit the code to show the plot in the proper way? Do I have to sort the dictionary to plot dates chronologically, or will it place them in the right spot automatically?

  1. Link 1 - Date tick labels
  2. Link 2 - matplotlib.pyplot.plot_date
  3. Link 3 - Date Demo Rrule
  4. Link 4 - Dates API

Possible solution?

  1. Plotting dates and associated values from a dictionary with Matplotlib

1 Answer 1

0

Ok, so looking thru possible solution I decided to format my data from string DD-MM-YYYY format to tuple (YYYY,MM,DD). Then I forwarded this information to dictionary and unpack it with * as argument for datetime.date. The rest is just like in the example with the link.

The full code for my program:

Small chunk of data from new.txt (original is over 12MB):

=A=
=A=
XDD
29September201821:54
=F=
nice,urodzinymateriinieozywionej
29September201821:54
=A=
urodzinygaleriijurajskiejsaxd
29September201821:53
=A=
jakasgwiazdeczka
29September201821:53
=F=
czytakosamaona
29September201821:53
=F=
tenkocertjakoczescewentujakiegoswiekszego
29September201821:53
=F=
nieznaju
29September201821:52
=A=
koncertsylwiigrzeszczakbylxdnawetfajniegraja
29September201821:48
=A=

29September201821:48
=A=
XDD
29September201816:03
=F=
ladnie
29September201816:03
=F=
wbrystolu
29September201816:03
=F=
niewiemgdziejestem,brawo
29September201816:03
=F=
xD
29September201816:03
=F=
nawetnie
29September201816:03
=F=
anie
29September201816:03
=A=
OMGZUZKAsentaphoto.

29September201816:02
=F=
jawbathjestemzjarkiem
29September201816:02
=A=
Niebomalebylyxd
29September201816:02
=F=
cosznalazlaswtychjaskiniach?
29September201816:02
=A=
RondowolsztyniexD
29September201816:02
=A=
OMGZUZKAsentaphoto.

29September201816:02
=F=

29September201816:01
=A=

29September201816:01
=A=
Oh
29September201816:01
=F=
amniegorszeodrzucaja
29September201816:01
=F=
nie,boinnesazawszegorszenizzuzka
29September201816:01
=A=
Tytakmawiasz?XD
29September201816:00
=F=
kok
29September201815:51
=F=
xd
29September201815:51

Full code to work on the file:

import re
from matplotlib import pyplot as plt
import datetime

def month(date):
    if "January" in date:
        return 1
    elif "February" in date:
        return 2
    elif "March" in date:
        return 3
    elif "April" in date:
        return 4
    elif "May" in date:
        return 5
    elif "June" in date:
        return 6
    elif "July" in date:
        return 7
    elif "August" in date:
        return 8
    elif "September" in date:
        return 9
    elif "October" in date:
        return 10
    elif "November" in date:
        return 11
    elif "December" in date:
        return 12
    else:
        pass

messages = open("new.txt","r").read()
dates = []

date_2digits = re.finditer('^[0-9][0-9][A-Z][a-z]*[0-9]{6}[:][0-9]{2}', messages, flags=re.MULTILINE)
date_1digit  = re.finditer('^[0-9][A-Z][a-z]*[0-9]{6}[:][0-9]{2}', messages, flags=re.MULTILINE)

for _ in date_2digits:
    txt = str(_.group(0))
    dates.append((int(txt[-9:-5]),month(txt),int(txt[0:2])))

for _ in date_1digit:
    txt = str(_.group(0))
    dates.append((int(txt[-9:-5]), int(month(txt)), int(txt[0:1])))

dates_dict = {}

for entry in dates:
    date = datetime.date(*entry)
    if date not in dates_dict.keys():
        dates_dict[date]=int(1)
    elif date in dates_dict.keys():
        add = dates_dict[date] + 1
        dates_dict.update({date:add})

x = list(dates_dict.keys())
y = list(dates_dict.values())

#plt.plot_date(x,y,'.')
plt.bar(x,y)
plt.xticks(x, rotation='vertical')
plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

Answer, but I can accept it after two days. I wanted to share with it, maybe somebody will find it helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.