matplotlib數據可視化之畫布及子圖處理


I、畫布

1、pyplot.figure()函數

matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)
matplotlib.pyplot.subplot(*args, **kwargs)

2、主要參數說明

 

 3、官方文檔直達鏈接

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.figure.html?highlight=figure#matplotlib.pyplot.figure

4、相關代碼測試

1、圖形1

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import seaborn as sns
import numpy as np
import pandas as pd

'''
matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)[source]
'''

# 解決中文不能在圖片中顯示的問題
plt.rcParams['font.sans-serif'] = ['SimHei']
# 用來正常顯示負號
plt.rcParams['axes.unicode_minus'] = False

#
plt.figure()
x_range = np.arange(7)
y_range = np.random.randint(1, 7, size=7)
plt.plot(y_range)
plt.show()

 

 2、圖形2

#修改參數
plt.figure(figsize=(2, 2), dpi=100) x_range = np.arange(7) y_range = np.random.randint(1, 7, size=7) plt.plot(x_range, y_range) plt.show()

 

II、子圖切割

1、subplot函數方法

matplotlib.pyplot.subplot(*args, **kwargs)

2、主要參數

 

 3、官方文檔鏈接

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplot.html?highlight=subplot#matplotlib.pyplot.subplot

4、簡單測試

①圖形1

plt.figure(figsize=(8, 8))
x_range = np.arange(7)
y_range = np.random.randint(1, 7, size=7)
plt.subplot(221)
plt.plot(x_range, y_range)
plt.subplot(222)
plt.plot(x_range, np.random.randint(1, 7, size=7))
plt.subplot(223)
plt.plot(x_range, np.random.randint(1, 7, size=7))
plt.show()

 

②、圖形2

# 指定多個畫布,並創建多個子圖,並修改指定子圖
plt.figure(1, figsize=(8, 8))
x_range = np.arange(7)
y_range = np.random.randint(1, 7, size=7)
plt.subplot(221)
plt.plot(x_range, y_range)
plt.subplot(222)
plt.plot(x_range, np.random.randint(1, 7, size=7))
plt.subplot(223)
plt.plot(x_range, np.random.randint(1, 7, size=7))
# plt.show()

plt.figure(2, figsize=(4, 4))
plt.subplot(212)
plt.plot(x_range, np.random.randint(1,7,size=7))

# 修改指定畫布,及相關子圖
plt.figure(1)
plt.subplot(222)
plt.title('for testfigure')
plt.show()

 

 

 III、子圖疊加在主圖上

1、aexs函數

matplotlib.pyplot.axes(arg=None, **kwargs)

2、主要參數說明

 

3、官方文檔鏈接

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.axes.html?highlight=axes#matplotlib.pyplot.axes

4、測試

 

 

 

# 在主圖上疊加小圖形
# 列表前兩位數字表示為起始坐標,后兩位表示為圖形的橫縱比(長寬)
x_range = np.arange(7)
y_range = np.random.randint(1, 7, size=7)
plt.figure()
plt.plot(x_range, y_range)
plt.title('主圖')
plt.axes([0.2, 0.2, 0.2, 0.2])
plt.plot(x_range, y_range)
plt.axes([0.5, 0.5, 0.2, 0.2])
plt.plot(x_range, y_range)

 

 over!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM