簡體   English   中英

熊貓從每日到每月繪制 x 軸

[英]pandas plot x-axis from daily to monthly

我試圖繪制股票價格和波動率。 但是,在 x 軸上存在重疊。 有誰知道如何從“每天”到每月的正確頻率更新,或者是否有任何自動擬合功能? 謝謝

import tushare as ts
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 'sh' for shanghai A share, other single stocks are six-digit codes
Ahist = ts.get_hist_data('sh', start='2017-01-01', end='2018-02-12') 
dfA = pd.DataFrame(Ahist)
A_basic = dfA[['close','ma5', 'ma10','ma20','price_change','p_change']]
A_basic.sort_index()
fig = plt.figure(figsize = (15,12))
plt.xticks(pd.date_range(A_basic.index[0], A_basic.index[-1], freq = '30D'))
fig1 = fig.add_subplot(2,1,1, xlabel='Date', ylabel='Price')
fig2 = fig.add_subplot(2,1,2, xlabel='Date', ylabel='Volatility')
#fig.autofmt_xdate() #not working here
fig1.plot(A_basic['close'])
vol = A_basic['p_change'].rolling(5).std() * np.sqrt(5) #calc volatility
fig2.plot(vol)
plt.grid(True)
plt.show()

在此處輸入圖片說明

只是我自己想出來的。

索引類型是索引,而不是日期。 需要轉換。 添加:

A_basic.index = pd.to_datetime(A_basic.index) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM