Matplotlib---小白級教程(1)

1.畫布的創建和佈局–add_subplot()

簡單理解就是,先準備一塊畫布,並且設定好這塊畫布的佈局。你是準備畫一個圖表還是多個圖表、你是準備在畫布的靠左邊畫圖,還是右邊畫圖。


fig = plt.figure()
ax1 = fig.add_subplot(numrows, numcols, fignum)

  • numrows 代表你的畫布,打算分幾行
  • numcols代表你的畫布,打算分幾列
  • fignum代表你的畫布上的第幾個圖表


案例一:


import matplotlib.pyplot as plt


fig = plt.figure()
ax1 = fig.add_subplot(1, 3, 1)			# 第一個畫布
ax2 = fig.add_subplot(1, 3, 2)			# 第二個畫布
ax3 = fig.add_subplot(1, 3, 3)			# 第三個畫布
plt.show()

在這裏插入圖片描述

  • 如上add_subplot()函數的第一個參數都爲1,那麼整個畫布設置一行的意思
  • 第二個參數都爲3,說明是一張畫布都分3列
  • 第三個參數,分別是1/2/3,是值具體的圖表的第一個、第二個、第三個


案例二:


import matplotlib.pyplot as plt


fig = plt.figure()
ax1 = fig.add_subplot(1, 3, 1)
ax3 = fig.add_subplot(1, 3, 3)
plt.show()

在這裏插入圖片描述



案例三:

import matplotlib.pyplot as plt


fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax4 = fig.add_subplot(2, 2, 4)
plt.show()

在這裏插入圖片描述

案例四:

import matplotlib.pyplot as plt


fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax4 = fig.add_subplot(2, 2, 4)
plt.show()

在這裏插入圖片描述



2.X & Y 座標的區間設置

    def plot(self, xs, ys, *args, zdir='z', **kwargs):
        """
        Plot 2D or 3D data.

        Parameters
        ----------
        xs : 1D array-like
            x coordinates of vertices.						# X座標軸
        ys : 1D array-like
            y coordinates of vertices.						# y座標軸
        zs : scalar or 1D array-like
            z coordinates of vertices; either one for all points or one for
            each point.
        zdir : {'x', 'y', 'z'}
            When plotting 2D data, the direction to use as z ('x', 'y' or 'z');
            defaults to 'z'.
        **kwargs
            Other arguments are forwarded to `matplotlib.axes.Axes.plot`.
        """

import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)
ax1.plot([0, 1, 2], [1, 2, 3])	
ax2.plot([0, 1, 2, 3], [1, 2, 3, 4])

plt.show()

  • 如上第一個圖中,x座標用[0, 1, 2]區間表示,y座標用[1, 2, 3]區間表示
  • 同理,第二個圖

在這裏插入圖片描述



3.表格名稱以及X & Y座標的備註

案例一:

import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)
ax1.plot([0, 1, 2], [1, 2, 3])
ax2.plot([0, 1, 2, 3], [1, 2, 3, 4])

ax1.set_title("table title")			# 圖一的標題
ax1.set_xlabel("x label")				# x座標的含義
ax1.set_ylabel("y label")				# y座標的含義
ax2.set_title("table title 2")
ax2.set_xlabel("x label 2")
ax2.set_ylabel("y label 2")
plt.show()


在這裏插入圖片描述

如果是一個畫布上有一張圖或多張圖,可以用如上方式。



案例二:

import matplotlib.pyplot as plt

fig = plt.figure()

ax2 = fig.add_subplot(1, 2, 2)		# 畫布顯示了第二個區域的圖表
ax2.plot([0, 1, 2, 3], [1, 2, 3, 4])

plt.xlabel('x info')
plt.ylabel('y info')
plt.title('Title Demo')

plt.show()

在這裏插入圖片描述

  • 此部分只顯示了一張圖的座標備註信息,如果是兩個圖表,建議還是利用案例一里面的方法設置座標的備註信息。

4.波動曲線的設置(顏色、線型、標註)

1.顏色篇


import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)
ax1.plot([0, 1, 2], [1, 2, 3], "red")				# 第三個參數,默認是顏色
ax2.plot([0, 1, 2, 3], [1, 2, 3, 4], "green")


plt.show()

在這裏插入圖片描述

別名 全名 顏色
b blue 藍色
g green 綠色
r red 紅色
y yellow 黃色
c Cyan 青色
k blacK 黑色
m Magenta 洋紅色
w waite 白色

使用別名和全名,都可以實現顏色的設置

2.線型篇

import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)
ax1.plot([0, 1, 2], [1, 2, 3], "r-")				# 使用別名的方式
ax2.plot([0, 1, 2, 3], [1, 2, 3, 4], "g--")


plt.show()

在這裏插入圖片描述

  • 如果要求不多,可以使用顏色的別名 + “–” 的形式,可以實現虛線的設置,而"-"則是普通的實線

具體線型的列表如下:

線條風格linestyle或ls 描述
‘-‘ 實線
‘:’ 點線
‘–’ 破折線
‘None’,’ ‘,’’ 什麼都不畫
‘-.’ 點劃線
‘--’ 虛線

3.標註篇

如上部分,我們看到的都是線型圖表,通過線把不同的點連接一起最終看到的效果。但有時候,我們需要的僅僅是標註所有點的情況,並不需要把所有點都連接到一起,那麼只需要使用標註功能即可。

import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)
ax1.plot([0, 1, 2], [1, 2, 3], "rx")		# rx對應的意思是,r 以及  x,一個表示顏色,一個表示X形狀標註
ax2.plot([0, 1, 2, 3], [1, 2, 3, 4], "g^")	# g表示綠色,^表示的是上三角形狀標註


plt.show()

在這裏插入圖片描述





所有標註的形狀如下:

標記maker 描述
‘o’ 圓圈
‘.’
‘D’ 菱形
‘s’ 正方形
‘h’ 六邊形1
‘*’ 星號
‘H’ 六邊形2
‘d’ 小菱形
‘_’ 水平線
‘v’ 一角朝下的三角形
‘8’ 八邊形
‘<’ 一角朝左的三角形
‘p’ 五邊形
‘>’ 一角朝右的三角形
‘,’ 像素
‘^’ 一角朝上的三角形
‘+’ 加號
‘\ ‘ 豎線
‘None’,’’,’ ‘
‘x’ X



5.plot()的其他屬性

alpha 浮點值
animated [True / False]
antialiased [True / False]
clip_box matplotlib.transform.Bbox 實例
clip_on [True / False]
clip_path Path 實例, Transform,以及Patch實例
color 任何 matplotlib 顏色
contains 命中測試函數
dash_capstyle [‘butt’ / ‘round’ / ‘projecting’]
dash_joinstyle [‘miter’ / ‘round’ / ‘bevel’]
dashes 以點爲單位的連接/斷開墨水序列
data (np.array xdata, np.array ydata)
figure matplotlib.figure.Figure 實例
label 任何字符串
linestyle [ ‘-’ / ‘--’ / ‘-.’ / ‘:’ / ‘steps’ / ...]
linewidth 以點爲單位的浮點值
lod [True / False]
marker [ ‘+’ / ‘,’ / ‘.’ / ‘1’ / ‘2’ / ‘3’ / ‘4’ ]
markeredgecolor 任何 matplotlib 顏色
markeredgewidth 以點爲單位的浮點值
markerfacecolor 任何 matplotlib 顏色
markersize 浮點值
markevery [ None / 整數值 / (startind, stride) ]
picker 用於交互式線條選擇
pickradius 線條的拾取選擇半徑
solid_capstyle [‘butt’ / ‘round’ / ‘projecting’]
solid_joinstyle [‘miter’ / ‘round’ / ‘bevel’]
transform matplotlib.transforms.Transform 實例
visible [True / False]
xdata np.array
ydata np.array
zorder 任何數值
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章