天天看點

subplots、add_subplots、math.floor()、math.ceil()函數math.floor()函數math.ceil()函數figure文法及操作

math.floor()函數

math.floor(x)函數為向下取整,傳回一個小于等于x的最大整數

math.ceil()函數

math.ceil(x)函數為向上取整,傳回一個大于等于x的最小整數

figure文法及操作

1. figure文法說明

figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)

num:圖像編号或名稱,數字為編号 ,字元串為名稱
figsize:指定figure的寬和高,機關為英寸;
dpi參數指定繪圖對象的分辨率,即每英寸多少個像素,預設值為80      1英寸等于2.5cm,A4紙是 21*30cm的紙張 
facecolor:背景顔色
edgecolor:邊框顔色
frameon:是否顯示邊框
           

2. subplots文法

subplot(nrows,ncols,sharex,sharey,subplot_kw,**fig_kw)

subplots、add_subplots、math.floor()、math.ceil()函數math.floor()函數math.ceil()函數figure文法及操作

【例子】:

import numpy as np
import matplotlib.pyplot as plt
 
x = np.arange(0, 100)
#劃分子圖
fig,axes=plt.subplots(2,2)
ax1=axes[0,0]
ax2=axes[0,1]
ax3=axes[1,0]
ax4=axes[1,1]
 
#作圖1
ax1.plot(x, x)
#作圖2
ax2.plot(x, -x)
#作圖3
ax3.plot(x, x ** 2)
ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#作圖4
ax4.plot(x, np.log(x))
plt.show()
           

3. add_subplots文法

add_subplot()的作用與subplot一樣,用于新增子圖。具體如下:

#建立figure對象

fig=plt.figure()

#建立子圖1,(2,2,1)表示建立2x2子圖中的第一個

ax1=fig.add_subplot(2,2,1)

【例子】:

import numpy as np
    import matplotlib.pyplot as plt
    x = np.arange(0, 100)
    #建立figure對象
    fig=plt.figure()
    #建立子圖1
    ax1=fig.add_subplot(2,2,1)
    ax1.plot(x, x)
    #建立子圖2
    ax3=fig.add_subplot(2,2,2)
    ax3.plot(x, x ** 2)
    ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
    #建立子圖3
    ax4=fig.add_subplot(2,2,3)
    ax4.plot(x, np.log(x))
    plt.show()
           

繼續閱讀