成为华尔街金融巨鳄第四课:Matplotlib从入门到放弃
生活随笔
收集整理的這篇文章主要介紹了
成为华尔街金融巨鳄第四课:Matplotlib从入门到放弃
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
成為華爾街金融巨鱷第四課:Matplotlib從入門到放棄
# 導包 import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np # 默認情況下,mpl不支持中文,我們需要進行一下設置 mpl.rcParams['font.family'] = 'SimHei' mpl.rcParams['axes.unicode_minus'] = False一、入門
plt.plot([1,2,3,4],[2,3,1,7],'*-.r') # 繪圖函數(折線圖)兩個參數x,y(list或數組),第三個參數決定圖像樣式(自行探索) plt.show() # 顯示函數二、深入
調用show之前的所有plot的圖像會在同一張圖中顯示出來
plt.plot([1,2,3,4],[2,3,1,7],'*-.r') plt.plot([1,2,3,4],[3,5,1,0],'o-b') plt.show() # 顯示函數常見的圖像標注函數
plt.plot([1,2,3,4],[2,3,1,7],'*-.r',label='LINE A') #label設置圖例 plt.plot([1,2,3,7],[3,5,1,0],'o-b',label='LINE B') # 設置圖像標題 plt.title("測試標題")# 設置x軸 plt.xlabel("測試X軸")# 設置y軸 plt.ylabel("測試Y軸")# 設置x軸范圍 plt.xlim(0,10)# 設置y軸范圍 plt.ylim(0,8)# 設置x軸刻度(可傳列表可傳數組) plt.xticks(np.arange(0,10,2))# 設置y軸刻度(第二個參數可將對應的刻度換成指定的形式) plt.yticks([0,1,3,5,7],['a','b','c','d','e'])# 顯示圖例 plt.legend()plt.show()三、和pandas一起搞
import pandas as pd # 參數parse_dates=True將所有能用時間對象表示的列統統轉為時間對象 df = pd.read_csv('maotai.csv',index_col=0,thousands=',',parse_dates=True)[['開盤','收盤','高','低']] df| 1778.00 | 1773.78 | 1785.05 | 1767.00 |
| 1752.93 | 1769.60 | 1769.60 | 1741.50 |
| 1790.01 | 1753.99 | 1795.00 | 1735.00 |
| 1819.98 | 1790.01 | 1827.87 | 1782.00 |
| 1820.00 | 1820.10 | 1830.80 | 1802.05 |
| ... | ... | ... | ... |
| 1715.00 | 1693.65 | 1720.53 | 1683.16 |
| 1740.00 | 1715.80 | 1742.35 | 1701.07 |
| 1711.00 | 1730.05 | 1730.05 | 1697.26 |
| 1724.00 | 1705.00 | 1728.88 | 1691.00 |
| 1730.01 | 1734.79 | 1750.00 | 1722.27 |
244 rows × 4 columns
DataFrame支持直接繪圖
df.plot() plt.show()作業:
使用Matplotlib模塊在一個窗口中繪制數學函數y=x,y=x2,y=x3+5x^2+2x+1的圖像,使用不同顏色的線加以區別,并使用圖例說明各個線代表什么函數。
我的答案
x=np.linspace(-10,10,10000) plt.plot(x,x,'-r',label="y=x") plt.plot(x,x**2,':b',label="y=x^2") plt.plot(x,3*x**3+5*x**2+2*x+1,'-.y',label="y=3x^3+5x^2+2x+1") plt.legend() plt.show()標準答案
x = np.linspace(-1000,1000,100000) y1 = x.copy() y2 = x**2 y3 = 3*x**3+5*x**2+2*x+1 plt.plot(x,y1,color='red',label='y=x') plt.plot(x,y2,color='green',label='y=x^2') plt.plot(x,y3,color='blue',label='y=3*x^3+5x^2+2x+1') plt.xlim(-1000,1000) plt.ylim(-1000,1000) plt.legend() plt.show()四、Matplotlib畫布與子圖
提問:如何用一個show畫出兩張圖
# 創建畫布 fig = plt.figure() # 在畫布上創建子圖 參數221的意思:將畫布分成2行2列,該子圖占第1個格子 ax1 = fig.add_subplot(2,2,1) ax2 = fig.add_subplot(2,2,2) # 在子圖上畫圖 ax1.plot([1,2,3,4],[4,3,2,1]) plt.show()五、柱狀圖與餅圖
### 柱狀圖兩個參數 第一個參數是柱子中心的位置 plt.bar([0,1,2,4],[5,6,7,8]) plt.show() # data = [62,48,56,100] labels = ['Jan','Feb','Mar','Apr']plt.bar(np.arange(len(data)),data,color='red',width=0.3) plt.xticks(np.arange(len(data)),labels) plt.show() ### 餅圖 autopct填入格式化字符串,表示餅圖上的數據以指定形式出現,explode表示各個板塊突出相對于中心多少 plt.pie([10,20,30,40],labels=['a','b','c','d'],autopct="%.2f%%",explode=[0,0.1,0,0.1]) plt.show()六、繪制K線圖
matplotlib.finanace中提供了大量繪制金融相關圖的函數接口
繪制K線圖:matplotlib.finanace.candlestick_ochl函數
完蛋了,家人們!這個包沒了!!
終端輸入 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade mpl_finance
更新完畢后,再輸入pip install --upgrade mplfinance
更新一下,把包找回來,按照下面的方式導入
import mpl_finance as fin mpl.use('Qt5Agg')#必須顯式指明matplotlib的后端 from matplotlib.dates import date2num df| 1778.00 | 1773.78 | 1785.05 | 1767.00 |
| 1752.93 | 1769.60 | 1769.60 | 1741.50 |
| 1790.01 | 1753.99 | 1795.00 | 1735.00 |
| 1819.98 | 1790.01 | 1827.87 | 1782.00 |
| 1820.00 | 1820.10 | 1830.80 | 1802.05 |
| ... | ... | ... | ... |
| 1715.00 | 1693.65 | 1720.53 | 1683.16 |
| 1740.00 | 1715.80 | 1742.35 | 1701.07 |
| 1711.00 | 1730.05 | 1730.05 | 1697.26 |
| 1724.00 | 1705.00 | 1728.88 | 1691.00 |
| 1730.01 | 1734.79 | 1750.00 | 1722.27 |
244 rows × 4 columns
# data2num將pythondatetime對象轉換成數組 df['time']=date2num(df.index.to_pydatetime()) df| 1778.00 | 1773.78 | 1785.05 | 1767.00 | 18943.0 |
| 1752.93 | 1769.60 | 1769.60 | 1741.50 | 18942.0 |
| 1790.01 | 1753.99 | 1795.00 | 1735.00 | 18941.0 |
| 1819.98 | 1790.01 | 1827.87 | 1782.00 | 18940.0 |
| 1820.00 | 1820.10 | 1830.80 | 1802.05 | 18939.0 |
| ... | ... | ... | ... | ... |
| 1715.00 | 1693.65 | 1720.53 | 1683.16 | 18584.0 |
| 1740.00 | 1715.80 | 1742.35 | 1701.07 | 18583.0 |
| 1711.00 | 1730.05 | 1730.05 | 1697.26 | 18582.0 |
| 1724.00 | 1705.00 | 1728.88 | 1691.00 | 18579.0 |
| 1730.01 | 1734.79 | 1750.00 | 1722.27 | 18578.0 |
244 rows × 5 columns
fig = plt.figure() ax = fig.add_subplot(1,1,1) # np.asarray(df) arr = df[['time','開盤','收盤','高','低']].values fin.candlestick_ochl(ax,arr) fig.show()[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳
fin.candlestick_ochl(ax,arr) ax為子圖,表示在該子圖上作K線圖,arr傳入數組對象,共5組數據分別為時間、開盤數據、收盤數據、最高、最低,即對應o(open)c(close)h(high)l(low)
總結
以上是生活随笔為你收集整理的成为华尔街金融巨鳄第四课:Matplotlib从入门到放弃的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的资源管理器,你是怎么了?(续)
- 下一篇: 模式识别技术