自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Matplotlib入門篇,也可以很酷炫

開發(fā) 后端
哈嘍,大家好。今天寫一篇 Matplotlib 的入門教程。Matplotlib 是 Python 數(shù)據(jù)可視化庫,廣泛應(yīng)用在數(shù)據(jù)分析和機(jī)器學(xué)習(xí)中。

[[441338]]

哈嘍,大家好。今天寫一篇 Matplotlib 的入門教程。

Matplotlib 是 Python 數(shù)據(jù)可視化庫,廣泛應(yīng)用在數(shù)據(jù)分析和機(jī)器學(xué)習(xí)中。

1. 第一張圖

Matplotlib 支持面向?qū)ο蠛蚿yplot接口兩種方式畫圖。

以這兩種方式為例,畫出如下圖所示的函數(shù)圖。

y=x^2

面向?qū)ο蠓绞?/h3>
  1. import matplotlib.pyplot as plt 
  2. import numpy as np 
  3.  
  4. x = np.linspace(0, 2, 100) 
  5.  
  6. fig, ax = plt.subplots() 
  7. ax.plot(x, x**2) # 折線圖 
  8. ax.set_xlabel('x') # 設(shè)置橫坐標(biāo)名稱 
  9. ax.set_ylabel('y') # 設(shè)置縱坐標(biāo)標(biāo)簽 
  10. ax.set_title("y = x^2") # 設(shè)置標(biāo)題 
  11.  
  12. plt.show() 

plt.subplots() 函數(shù)返回fig和ax,分別是Figure對象和Axes對象。前者代表畫布,后者代表畫布上的繪圖區(qū)域,很顯然畫布和繪圖區(qū)域是一對多的關(guān)系。

之后關(guān)于繪圖的設(shè)置,都通過Axes對象完成。

pyplot方式

  1. import matplotlib.pyplot as plt 
  2. import numpy as np 
  3.  
  4. x = np.linspace(0, 2, 100) 
  5.  
  6. plt.figure() 
  7. plt.plot(x, x**2) 
  8. plt.xlabel('x'
  9. plt.ylabel('y'
  10.  
  11. plt.show() 

pyplot方式繪圖和設(shè)置都通過plt來完成,沒有對象的概念。

雖然這兩種方式都能畫圖,但官方更建議采用面向?qū)ο蟮姆绞健?/p>

2. 支持多種圖形

除了上面例子中看到的折線圖,Matplotlib 還支持以下圖形:

  • stackplot:堆疊圖
  • bar/barh:柱狀圖
  • hist:直方圖
  • pie:餅形圖
  • scatter:散點圖
  • contourf:等高線圖
  • boxplot:箱型圖
  • violinplot:提琴圖

另外,Matplotlib 還是支持 3D 繪圖

3. 常見設(shè)置

在第一小節(jié)的例子里,我們通過set_xlabel和set_title設(shè)置坐標(biāo)軸名稱和標(biāo)題。

除此之外,還可以添加注釋和圖例。

  1. x = np.linspace(0, 2, 100) 
  2.  
  3. fig, ax = plt.subplots() 
  4. ax.plot(x, x**2, label='二次函數(shù)'
  5. ax.set_xlabel('x'
  6. ax.set_ylabel('y'
  7. ax.set_title("y = x^2"
  8.  
  9. # 添加注釋 
  10. ax.annotate('坐標(biāo)(1,1)', xy=(1, 1), xytext=(0.5, 1.5), 
  11.             arrowprops=dict(facecolor='black', shrink=0.05)) 
  12. # 添加圖例 
  13. ax.legend() 

還可以設(shè)置坐標(biāo)軸的格式

  1. ax.xaxis.set_major_formatter('x坐標(biāo){x}'

如果坐標(biāo)軸是日期會非常有用,可以將日期轉(zhuǎn)成周、月、季度等格式。

4. 一個畫布多圖形前面提到過,一個畫布可以有多個繪圖區(qū)域。

下面使用plt.subplots()函數(shù)可以創(chuàng)建2行2列,4個繪圖區(qū)域。

  1. import matplotlib.pyplot as plt 
  2. import numpy as np 
  3.  
  4. fig, axs = plt.subplots(ncols=2, nrows=2, figsize=(5.5, 3.5), 
  5.                         constrained_layout=True
  6. add an artist, in this case a nice label in the middle... 
  7. for row in range(2): 
  8.     for col in range(2): 
  9.         axs[row, col].annotate(f'axs[{row}, {col}]', (0.5, 0.5), 
  10.                                transform=axs[row, col].transAxes, 
  11.                                ha='center', va='center', fontsize=18, 
  12.                                color='darkgrey'
  13. fig.suptitle('plt.subplots()'

也可以通過subplot_mosaic()函數(shù)創(chuàng)建

  1. fig, axd = plt.subplot_mosaic([['upper left''upper right'], 
  2.                                ['lower left''lower right']], 
  3.                               figsize=(5.5, 3.5), constrained_layout=True
  4. for k in axd: 
  5.     annotate_axes(axd[k], f'axd["{k}"]', fontsize=14) 
  6. fig.suptitle('plt.subplot_mosaic()'

通過subplot_mosaic()函數(shù),還可以將其他幾個繪圖區(qū)域合并成一個。

  1. fig, axd = plt.subplot_mosaic([['upper left''right'], 
  2.                                ['lower left''right']], 
  3.                               figsize=(5.5, 3.5), constrained_layout=True
  4. for k in axd: 
  5.     annotate_axes(axd[k], f'axd["{k}"]', fontsize=14) 
  6. fig.suptitle('plt.subplot_mosaic()'

通過 GridSpec 也可以創(chuàng)建更復(fù)雜的繪圖區(qū)域。

  1. fig = plt.figure(constrained_layout=True
  2. gs0 = fig.add_gridspec(1, 2) 
  3.  
  4. gs00 = gs0[0].subgridspec(2, 2) 
  5. gs01 = gs0[1].subgridspec(3, 1) 
  6.  
  7. for a in range(2): 
  8.     for b in range(2): 
  9.         ax = fig.add_subplot(gs00[a, b]) 
  10.         annotate_axes(ax, f'axLeft[{a}, ]', fontsize=10) 
  11.         if a == 1 and b == 1: 
  12.             ax.set_xlabel('xlabel'
  13. for a in range(3): 
  14.     ax = fig.add_subplot(gs01[a]) 
  15.     annotate_axes(ax, f'axRight[{a}, ]'
  16.     if a == 2: 
  17.         ax.set_ylabel('ylabel'
  18.  
  19. fig.suptitle('nested gridspecs'

5. 高級用法

Matplotlib 很強(qiáng)大,設(shè)置很靈活,比如,折線圖可以用極坐標(biāo)畫圖

稍加改造還可以畫出玫瑰圖。

折線圖隱藏坐標(biāo)軸和邊框,再結(jié)合注釋就可以畫出時間軸

多圖組合形成更復(fù)雜的統(tǒng)計圖

Matpolitlib還支持圖形動畫和交互式。

今天這篇文章只介紹了 Maptplotlib 很初級的一部分內(nèi)容,它本身內(nèi)容非常豐富、也很復(fù)雜。后面有機(jī)會我們可以介紹更深入的內(nèi)容。

 

如果本文對你有用就點個 在看 鼓勵一下吧。

 

責(zé)任編輯:武曉燕 來源: 渡碼
相關(guān)推薦

2011-01-18 17:00:31

Postfix入門

2017-09-12 10:26:47

springbootmaven結(jié)構(gòu)

2020-11-16 10:19:33

Java

2016-09-06 17:43:12

SwiftCloudKit開發(fā)

2009-06-09 13:02:30

NetBeans使用教程

2022-03-28 09:31:58

for循環(huán)語句

2022-01-27 09:35:45

whiledo-while循環(huán)Java基礎(chǔ)

2020-11-13 07:22:46

Java基礎(chǔ)While

2015-07-30 09:43:10

獨立游戲開發(fā)入門

2012-01-17 10:47:07

jQuery

2022-07-06 07:57:37

Zookeeper分布式服務(wù)框架

2020-11-09 10:19:05

Java

2020-11-19 10:36:16

Java基礎(chǔ)方法

2022-03-10 09:33:21

Java數(shù)組初始化

2018-12-21 12:25:08

2010-09-08 13:42:06

2009-06-15 17:22:36

JBoss Seam

2017-01-22 21:30:39

大數(shù)據(jù)Kaggle函數(shù)

2010-08-31 14:01:23

iPhone

2010-05-20 19:12:37

點贊
收藏

51CTO技術(shù)棧公眾號