python數據可視化——matplotlib 用戶手冊入門:使用指南

參考matplotlib官方指南:html

https://matplotlib.org/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-pypython

matplotlib的用戶指南分爲三個等級:入門,中級,高級。在入門級,主要介紹下圖內容dom

這裏我首先介紹 使用指南 部分,即 Usage Guideide

1.一張圖的組成函數

在使用matplotlib畫圖時,你會發現各類參數,下面就說說這些參數具體設置什麼ui

 

2.畫圖函數的輸入數據類型spa

最好將輸入數據轉換爲np.array類型code

如,將pandas.DataFrame轉換爲 np.arrayhtm

1 a = pandas.DataFrame(np.random.rand(4,5), columns = list('abcde')) 2 a_asndarray = a.values

將 np.matrix 轉換爲 np.arrayblog

1 b = np.matrix([[1,2],[3,4]]) 2 b_asarray = np.asarray(b)

3.matplotlib,pyplot 與 pylab的關係

pyplot是matplotlib的一個模塊,pylab是與matplotlib共同安裝的模塊

4.自定義畫圖函數

若是現有的畫圖形狀不知足需求,能夠方便地定義畫圖函數

 

 1 def my_plotter(ax, data1, data2, param_dict):  2     """
 3  A helper function to make a graph  4 
 5  Parameters  6  ----------  7  ax : Axes  8  The axes to draw to  9 
10  data1 : array 11  The x data 12 
13  data2 : array 14  The y data 15 
16  param_dict : dict 17  Dictionary of kwargs to pass to ax.plot 18 
19  Returns 20  ------- 21  out : list 22  list of artists added 23     """
24     out = ax.plot(data1, data2, **param_dict) 25     return out 26 
27 # which you would then use as:
28 
29 data1, data2, data3, data4 = np.random.randn(4, 100) 30 fig, ax = plt.subplots(1, 1) 31 my_plotter(ax, data1, data2, {'marker': 'x'})

 

5.交互模式

能夠經過matplotlib.interactive(),matplotlib.is_interactive(),matplotlib.pyplot.ion()能夠打開交互模式

使用 matplotlib.is_interactive()能夠關閉交互模式

在ipython中運行如下代碼:

1 import matplotlib.pyplot as plt 2 plt.ion() 3 plt.plot([1.6, 2.7]) 4 ax = plt.gca() 5 ax.plot([3.1, 2.2]) 6 plt.draw()

相關文章
相關標籤/搜索