Python time series Drawing plot summary

Source: Internet
Author: User
Tags pandas read csv

Drawing intuitively, it is essential to further select a time series analysis model, in order to show more clearly the regularity of the timing data (including the trend, the regularity of the change (one week, one months, one year, and so on) and the periodic pattern. The following is mainly based on the pandas library summarizes what common diagrams can be used for analysis. There are a total of the following:

    • Linear chart
    • Histogram and density graphs
    • Box-shaped diagram
    • Heat Force diagram
    • Lag graph
    • Scatter chart
    • Self-correlation diagram

(1) Linear chart

This is the most basic diagram, the horizontal axis is the time, the longitudinal axes is a variable, describes the variable over time, the diagram is obviously easy to find the above potential law. Directly on the code:

# -*-coding:utf-8-*-  from Import Series Import  = series.from_csv ('minimum.csv', header=0) data.astype (float)  Print(Data.head ()) Data.plot (Style='r '). ' ) plt.show ()

You can also just look at one of the year's, say 1990, as follows:

data = Series.from_csv ('minimum.csv', header== data['1990 ' ]one_year.plot ()

One problem with this solution is that the object type is not plot, view pandas read csv file Typeerror:empty ' DataFrame ': No numeric data to plot

In addition, the style of plot can be viewed by the document itself to choose Favorite, Document Link

(2) Histogram and density map

Histogram, you know, he has no timing, just in a time range of variable range statistics, such as the data is divided into 10 bins, we will see the number of each bin (for example, how many days, months, etc.), this statistical method is the same as the density map, you can see the variables in which range of values are more, What is less and so on, observes the latent distribution law of the data.

 from Import Series Import  = series.from_csv ('minimum.csv', header=0) data.hist () plt.show () Data.plot (Kind='KDE') plt.show ()

There is a major problem here, is what is Kde:kernel Density estimation,sklearn Tutorial will tell you, please read the document

(3) box-shaped diagram

 from Import *import= series.from_csv ('minimum.csv', header=  = Data.groupby (Timegrouper ('A'= DataFrame ()for   in groups:    years[name.year]=group.valuesyears.boxplot () plt.show ()

Put one here:

By the way, the box chart, it can show a set of data, the maximum, minimum, median, and up and down four, the most important is the minimum value of the maximum is given in the upper and lower four sub-number of a certain interval, forming a box and beard (so also called Box plot), for example, 1981, the maximum value is 20-25 , the minimum value is between 0-5, it is important to remember that in a large probability interval is the largest minimum, not the actual maximum minimum, leaving the interval will have a lot of small circles and * representation, the circle represents outliers, * denotes extreme values.

Of course, you can also take out one year to analyze:

 fromPandasImport*ImportMatplotlib.pyplot as Pltdata= Series.from_csv ('Minimum.csv', header=0) Data= data['1990']groups= Data.groupby (Timegrouper ('M')) months= Concat ([DataFrame (X[1].values) forXinchGroups], Axis=1) months=DataFrame (months) Months.columns= Range (1,13) Months.boxplot () plt.show ()

(4) Heat Force diagram

Heat is more image point, although we do not know the value, but through the color we can look at the distribution of extreme values, the more vivid color, the greater the value (red and yellow), the more dim the color, the smaller the value (blue-green), of course, the display may not be the same.

#1988-year example fromPandasImport*ImportMatplotlib.pyplot as Pltdata= Series.from_csv ('Minimum.csv', header=0) Data= data['1988']groups= Data.groupby (Timegrouper ('M')) months= Concat ([DataFrame (X[1].values) forXinchGroups], Axis=1) months=DataFrame (months) Months.columns= Range (1,13) plt.matshow (months,interpolation=none,aspect='Auto') plt.show ()

(5) Lag graphs and scatter plots

It should be noted here that the lag chart and scatter plot can be drawn according to time series, that is (1) Inside the line chart, here is actually not the timing, but the analysis of data correlation, we give the data for a period of observation interval, because it is assumed that the previous data is related to the subsequent data, is positive correlation or negative correlation or what, Select an interval and plot a bit.

 from Import *import  matplotlib.pyplot as Pltfromimport= Series.from_csv ('minimum.csv', header=0) lag_plot (data) plt.show ()

Obviously we see a positive correlation. Here is a one-week scatter plot, we can see the interval of one day, two days, three days ...

 fromPandasImport*ImportMatplotlib.pyplot as Pltdata= Series.from_csv ('Minimum.csv', header=0) Values=DataFrame (data.values) Lags= 7Columns=[Values] forIinchRange (1, (lags + 1): Columns.Append (Values.shift (i)) Dataframe= concat (Columns, Axis=1) Columns= ['T'] forIinchRange (1, (lags + 1)): Columns.Append ('T -'+str (i)) Dataframe.columns=Columnsplt.figure (1) forIinchRange (1, (lags + 1): Ax= Plt.subplot (240 +i) Ax.set_title ('T vs t'+str (i)) Plt.scatter (x=dataframe['T'].values, y=dataframe['T -'+Str (i)].values) plt.show ()

(6) Self-correlation diagram

This needless to say, is to consider the correlation between data and a certain interval of data, the closer to 1 is positive correlation, close to 1 is negative correlation, close to 0 is very low correlation. Here is the calculation formula, the following code gives the autocorrelation default is the data and lag 1 step correlation.

 from Import Series Import Matplotlib.pyplot as Plt  from Import  = series.from_csv ('minimum.csv', header=0) Autocorrelation_plot (Series ) Plt.show ()

Of course, these are not introduced completely, the detailed version is still attached to the following:

Data for all graphs

Python time series Drawing plot summary

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.