Tushare stock data visualization (latest version)

Preface

The previous blog introduced how to use Tushare to download stock data. Using Tushare to obtain stock data (latest updated version) - CSDN blog , then this blog will introduce how to visualize the downloaded data.


Download Document

We have saved the screened stock data to the local computer. Here we will take the Shanghai Pudong Development Bank stock with the stock code 600000.SH as an example.

Dependent library environment

import datetime
import pandas as pd
import numpy as np
from pandas.plotting import register_matplotlib_converters
# 数据可视化
import matplotlib.pyplot as plt
register_matplotlib_converters()

Among them, from pandas.plotting import register_matplotlib_converters, register_matplotlib_converters() is to match the time series type data to the time series in malplotlib, so that our final display image has the correct horizontal axis display.

Code

# 获取文件保存路径
data_path = r"你的文件路径"
# 把时间作为序列
df = pd.read_csv(data_path,index_col=0)
date = [str(time) for time in df.index]
# 将pd中的时间序列转换
df.index = list(map(lambda x:datetime.datetime.strptime(x,'%Y%m%d'), date))
print(df.index)

index_col=0 is to use the first column read from the file as the index. The corresponding original file here needs the first column as the transaction time, that is, the time is used as the index. If the first column of the downloaded original file is not the transaction time data, you can manually Move the time column to the first column, or directly obtain the time column through df['trade_date'] and then convert it, the effect is the same

'%Y%m%d' is a regular match. For example, the corresponding data is 20231128. The output result of using datetime.datetime.strptime is time series type data such as 2023-11-28.

DatetimeIndex(['2023-12-26', '2023-12-25', '2023-12-22', '2023-12-21',
               '2023-12-20', '2023-12-19', '2023-12-18', '2023-12-15',
               '2023-12-14', '2023-12-13',
               ...
               '2010-01-15', '2010-01-14', '2010-01-13', '2010-01-12',
               '2010-01-11', '2010-01-08', '2010-01-07', '2010-01-06',
               '2010-01-05', '2010-01-04'],
              dtype='datetime64[ns]', length=3351, freq=None)

The next step is to obtain the corresponding daily highest transaction price (corresponding field name 'high') and visual display

# 获取字段名为high的数据
data = np.array(df['high'].tolist())

# 可视化
plt.plot(df.index,data,label='high-data')
plt.title("600000.SH")
plt.legend()
plt.show()

Everyone is welcome to discuss and exchange~


Guess you like

Origin blog.csdn.net/weixin_57506268/article/details/135268470