简体   繁体   English

使用Matplotlib从带有日期的字典中绘制数据

[英]Plotting data from dictionary with dates using Matplotlib

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. 但是,x轴是正常的一组值,而不是日期。 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. 它们是按顺序绘制的,没有按时间顺序保存在字典中.x轴上也没有实际比例尺,因此它不会在没有数据的日期上绘制空白空间。

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 链接1-日期刻度标签
  2. Link 2 - matplotlib.pyplot.plot_date 链接2-matplotlib.pyplot.plot_date
  3. Link 3 - Date Demo Rrule 链接3-日期演示规则
  4. Link 4 - Dates API 链接4-日期API

Possible solution? 可能的解决方案?

  1. Plotting dates and associated values from a dictionary with Matplotlib 使用Matplotlib从字典中绘制日期和相关值

Ok, so looking thru possible solution I decided to format my data from string DD-MM-YYYY format to tuple (YYYY,MM,DD). 好的,因此寻找可能的解决方案,我决定将数据从字符串DD-MM-YYYY格式格式化为元组(YYYY,MM,DD)。 Then I forwarded this information to dictionary and unpack it with * as argument for datetime.date. 然后,我将此信息转发给字典,并使用*作为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): 来自new.txt的一小部分数据(原始数据超过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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM