Python date formatting and plotting, python date plotting

Source: Internet
Author: User

Python date formatting and plotting, python date plotting

Drawing a curve of volume changes over time is a common requirement, for example, drawing a curve of the number of software users. Matplotlib. pyplot. plot_date (date, num) is the main function used to draw a curve that changes over time ). Since the first variable is of the datetime type, you must first format the string type data input.

1. Use strptime to format a string 1.1 Basic example

Time = datetime. datetime. strptime ('2017-12-28 13:49:30 ',' % Y-% m-% d % H: % M: % s ')

Print time

13:49:30

1.2 common formatting Mappings

The common ing relationships are shown in the following table (13:49:30). For more information, see format the time and date in python-go to dawn-blog

Year

Month

Day

Hour

Minute

Second

% Y

% Y

% M

% B

% D

% H

% I

% M

% S

14

2014

12

Dec

28

13

01

49

30

2. A specific drawing example

Count the number of first-time software installations in windows, and draw a scatter chart (by day) and a bar chart (by year)

import _winregimport datetimeimport matplotlib.datesimport matplotlib.pyplot import numpy as npprint "import winreg done!"key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,r"Software\Microsoft\Windows\CurrentVersion\Uninstall")#key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Explorer")print keyall_software=[]k=0try:    i=0        while 1:        subkey_string=_winreg.EnumKey(key,i)        #print subkey_string        try:            j=0            temp={}            get_installDate=0            #some subkey may not have installdate subkey and value            while 1:                #subkey=_winreg.OpenKey(key,"InstallDate")                subkey=_winreg.OpenKey(key,subkey_string)                name, value, type_value = _winreg.EnumValue(subkey,j)                #print repr(name),repr(value)                #print "name is "+name                if name=="DisplayName":                    #print "Name equals DisplayName"                    temp["DisplayName"]=value                if name=="InstallDate":                    #print "Name equals installdate"                    temp["InstallDate"]=value                    get_installDate=1                j+=1        except WindowsError:            #print 'interruptted j='+str(j)            if get_installDate:                all_software.append(temp)        #print repr(name)        #print value        i +=1except WindowsError:    printfor item in range(len(all_software)):    all_software[item]["InstallDate"]=datetime.datetime.strptime(all_software[item]["InstallDate"].encode('ascii','ignore'),'%Y%m%d')count={}bar_by_year={}bar_by_year['2012']=0bar_by_year['2013']=0bar_by_year['2014']=0for item in all_software:    if count.has_key(item["InstallDate"]):        count[item["InstallDate"]]+=1    else:        count[item["InstallDate"]]=1    bar_by_year[str(item['InstallDate'].year)]+=1xvalues=[]yvalues=[]for key,value in count.iteritems():    xvalues.append(key)    yvalues.append(value)xvalues=matplotlib.dates.date2num(xvalues)matplotlib.pyplot.figure(1)matplotlib.pyplot.plot_date(xvalues,yvalues)matplotlib.pyplot.figure(2)xvalues_2=np.arange(3)yvalues_2=bar_by_year.values()fig2=matplotlib.pyplot.bar(xvalues_2,yvalues_2,0.5)matplotlib.pyplot.xticks(xvalues_2+0.25, ['2012','2013','2014'], rotation='vertical')matplotlib.pyplot.show()#value, type = _winreg.QueryValueEx(key, "EnableAutoTray")

 

 

Iii. References

[1] Python registry operations-normal-blog

[2] c ++-How can I enumerate/list all installed applications in Windows XP? -Stack Overflow

[3] winreg-Windows registry access-Python v3.0.1 documentation

[4] graph-plow.time in python with matplotlib-Stack Overflow

[5] Sha Hu Wang | matplotlib Plot 2

[6] Python Chart Drawing: matplotlib Drawing Library entry-diemeng Zhuangzhou-blog Park

[7] ticks_and_spines example code: ticklabels_demo_rotation.py-Matplotlib 1.4.2 documentation

[8] api example code: barchart_demo.py-Matplotlib 1.4.2 documentation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.