The most simple time series Python Visualization: scientific analysis of price trends data, forecast prices, exploration prices

Time series data in the data field of science is everywhere, in quantified financial sector is also very common, can be used to analyze price trends, forecast prices, exploring the price behavior.

Learn to time series data visualization, it can help us explore more intuitive time-series data, looking for potential law.

This article will use Python in matplotlib [1] library, and with examples to explain. matplotlib library is a ⼀ Use to create publication-quality graphs of tables ⾯ drawing package (2D graphics library), Python is the most basic visualization tools.

[Tools] Python 3

[Data] Tushare

[Note] attention is the example to explain the method, please be flexible.

01

Single time series

First of all, from the tushare.pro obtain Index daily market data and view the data type.

import tushare as ts
import pandas as pd

pd.set_option('expand_frame_repr', False)  # 显示所有列
ts.set_token('your token')
pro = ts.pro_api()

df = pro.index_daily(ts_code='399300.SZ')[['trade_date', 'close']]
df.sort_values('trade_date', inplace=True) 
df.reset_index(inplace=True, drop=True)

print(df.head())

  trade_date    close
0   20050104  982.794
1   20050105  992.564
2   20050106  983.174
3   20050107  983.958
4   20050110  993.879

print(df.dtypes)

trade_date     object
close         float64
dtype: object

Trading Time column 'trade_date' is not the time to type, and is not indexed, it needs to be transformed.

df['trade_date'] = pd.to_datetime(df['trade_date'])
df.set_index('trade_date', inplace=True)

print(df.head())

              close
trade_date         
2005-01-04  982.794
2005-01-05  992.564
2005-01-06  983.174
2005-01-07  983.958
2005-01-10  993.879

Next, the drawing can begin, we need to import matplotlib.pyplot [2], and then by setting set_xlabel () and set_xlabel () to tag the x-axis and y-axis.

import matplotlib.pyplot as plt

ax = df.plot(color='')
ax.set_xlabel('trade_date')
ax.set_ylabel('399300.SZ close')
plt.show()

The most simple time series Python Visualization: scientific analysis of price trends data, forecast prices, exploration prices

matplotlib library has many built-in chart styles to choose from, by printing plt.style.available see what are the specific options, when applied directly call plt.style.use ( 'fivethirtyeight') can be.

print(plt.style.available)

['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2', 'tableau-colorblind10', '_classic_test']

plt.style.use('fivethirtyeight')
ax1 = df.plot()
ax1.set_title('FiveThirtyEight Style')
plt.show()

The most simple time series Python Visualization: scientific analysis of price trends data, forecast prices, exploration prices

02

Set More details

上面画出的是一个很简单的折线图,其实可以在plot()里面通过设置不同参数的值,为图添加更多细节,使其更美观、清晰。

figsize(width, height)设置图的大小,linewidth设置线的宽度,fontsize设置字体大小。然后,调用set_title()方法设置标题。

ax = df.plot(color='blue', figsize=(8, 3), linewidth=2, fontsize=6)
ax.set_title('399300.SZ close from 2005-01-04 to 2019-07-04', fontsize=8)
plt.show()

The most simple time series Python Visualization: scientific analysis of price trends data, forecast prices, exploration prices

如果想要看某一个子时间段内的折线变化情况,可以直接截取该时间段再作图即可,如df['2018-01-01': '2019-01-01']

df_subset_1 = df['2018-01-01':'2019-01-01']
ax = df_subset_1.plot(color='blue', fontsize=10)
plt.show()

The most simple time series Python Visualization: scientific analysis of price trends data, forecast prices, exploration prices

如果想要突出图中的某一日期或者观察值,可以调用.axvline().axhline()方法添加垂直和水平参考线。

ax = df.plot(color='blue', fontsize=6)
ax.axvline('2019-01-01', color='red', linestyle='--')
ax.axhline(3000, color='green', linestyle='--')
plt.show()

The most simple time series Python Visualization: scientific analysis of price trends data, forecast prices, exploration prices

也可以调用axvspan()的方法为一段时间添加阴影标注,其中alpha参数设置的是阴影的透明度,0代表完全透明,1代表全色。

大家在学python的时候肯定会遇到很多难题,以及对于新技术的追求,这里推荐一下我们的Python资源分享秋秋裙:855408893 内有安装包,学习视频资料。这里是Python学习者的聚集地,零基础,进阶,都欢迎每日分享一些学习的方法和需要注意的小细节

ax = df.plot(color='blue', fontsize=6)
ax.axvspan('2018-01-01', '2019-01-01', color='red', alpha=0.3)
ax.axhspan(2000, 3000, color='green', alpha=0.7)
plt.show()

The most simple time series Python Visualization: scientific analysis of price trends data, forecast prices, exploration prices

03

移动平均时间序列

有时候,我们想要观察某个窗口期的移动平均值的变化趋势,可以通过调用窗口函数rolling来实现。下面实例中显示的是,以250天为窗口期的移动平均线close,以及与移动标准差的关系构建的上下两个通道线upper和lower。

ma = df.rolling(window=250).mean()
mstd = df.rolling(window=250).std()

ma['upper'] = ma['close'] + (mstd['close'] * 2)
ma['lower'] = ma['close'] - (mstd['close'] * 2)

ax = ma.plot(linewidth=0.8, fontsize=6)
ax.set_xlabel('trade_date', fontsize=8)
ax.set_ylabel('399300.SZ close from 2005-01-04 to 2019-07-04', fontsize=8)
ax.set_title('Rolling mean and variance of 399300.SZ cloe from 2005-01-04 to 2019-07-04', fontsize=10)
plt.show()

The most simple time series Python Visualization: scientific analysis of price trends data, forecast prices, exploration prices

04

多个时间序列

If you want to visualize a plurality of time-series data, it can also be called directly plot () method . Our example from tushare.pro selected three stocks above the daily market data analysis.

# 获取数据
code_list = ['000001.SZ', '000002.SZ', '600000.SH']
data_list = []
for code in code_list:
    print(code)
    df = pro.daily(ts_code=code, start_date='20180101', end_date='20190101')[['trade_date', 'close']]
    df.sort_values('trade_date', inplace=True)
    df.rename(columns={'close': code}, inplace=True)
    df.set_index('trade_date', inplace=True)
    data_list.append(df)
df = pd.concat(data_list, axis=1)
print(df.head())

000001.SZ
000002.SZ
600000.SH
            000001.SZ  000002.SZ  600000.SH
trade_date                                 
20180102        13.70      32.56      12.72
20180103        13.33      32.33      12.66
20180104        13.25      33.12      12.66
20180105        13.30      34.76      12.69
20180108        12.96      35.99      12.68

# 画图
ax = df.plot(linewidth=2, fontsize=12)
ax.set_xlabel('trade_date')
ax.legend(fontsize=15)
plt.show()

The most simple time series Python Visualization: scientific analysis of price trends data, forecast prices, exploration prices

Call .plot.area () method may generate time-series data in FIG area, cumulative total is displayed.

ax = df.plot.area(fontsize=12)
ax.set_xlabel('trade_date')
ax.legend(fontsize=15)
plt.show()

The most simple time series Python Visualization: scientific analysis of price trends data, forecast prices, exploration prices

To show separately in different sub-graphs each time series, by setting the parameter subplots = True achieved. layout specified number of rows to be used, sharex and sharey for setting whether to share the row and column, the colormap = 'viridis'  set a different color for each line.

df.plot(subplots=True,
          layout=(2, 2),
          sharex=False,
          sharey=False,
          colormap='viridis',
          fontsize=7,
          legend=False,
          linewidth=0.3)

plt.show()

The most simple time series Python Visualization: scientific analysis of price trends data, forecast prices, exploration prices

05

to sum up

This paper describes how to use the Python matplotlib library time-series data for some simple visualization, visualization comprising a single time series and disposed in the detail view, the visualization moving average time series and a plurality of time series.

Guess you like

Origin blog.51cto.com/14445003/2419992