欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?matplotlib設(shè)置多子圖、子圖間距及外邊距的幾種方式

 更新時間:2024年02月20日 11:48:11   作者:正在學(xué)習(xí)中的李斌  
子圖是Matplotlib中強大的功能之一,使用函數(shù)您可以方便地創(chuàng)建多個子圖,并使用Axes對象繪制各種圖形,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib設(shè)置多子圖、子圖間距及外邊距的幾種方式,需要的朋友可以參考下

Python matplotlib

設(shè)置多子圖

設(shè)置多子圖縱向間距

設(shè)置多子圖橫向間距

設(shè)置外邊距

1. 方式一。設(shè)置一個 2*2 的相同尺寸子圖,統(tǒng)一添加。

  • 可選參數(shù):sharex=True,sharey=False 共享X軸,不共享Y軸。
  • 可選參數(shù):facecolor 背景填充色。
  • figsize 圖片的大小。
import matplotlib.pyplot as plt

fig,axes = plt.subplots(2,2,figsize=(6,6),dpi=100,facecolor="w",sharex=True,sharey=False)

# 遍歷所有子圖
for i,ax in enumerate(axes.flatten()):
    ax.text(0.5, 0.5, i, fontdict={'fontsize':20,})
    
# 對單一子圖進(jìn)行操作    
axes[0,0].set_title('0,0 title')

2. 方式二。設(shè)置一個 2*2 子圖,單獨添加尺寸相同的子圖。

  • add_subplot 先創(chuàng)建 fig 再單獨添加子圖。
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(6,6),dpi=100,facecolor="w")

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

ax1.text(0.3, 0.5, '2,2,1', fontdict={'fontsize':20,})
ax4.set_title('2,2,4 title')  

3. 方式三。單獨設(shè)置尺寸不太的子圖。

subplot2grid 單獨設(shè)置子圖所占的行列比例。

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8,8),dpi=100,facecolor="w")

# 3*3 的網(wǎng)格;(0,0)位置開始,跨度為 1 行 1 列
ax1 = plt.subplot2grid((3,3),(0,0),colspan=1,rowspan=1)
# 3*3 的網(wǎng)格;(1,0)位置開始,跨度為 1 行 2 列
ax2 = plt.subplot2grid((3,3),(1,0),colspan=2,rowspan=1)
# 3*3 的網(wǎng)格;(1,2)位置開始,跨度為 2 行 1 列
ax3 = plt.subplot2grid((3,3),(1,2),colspan=1,rowspan=2)

ax1.text(0.1, 0.5, '(0,0) rows 1, cols 1', fontdict={'fontsize':12,})
ax2.text(0.3, 0.5, '(1,0) rows 1, cols 2', fontdict={'fontsize':12,})
ax3.text(0.1, 0.5, '(1,2) rows 2, cols 1', fontdict={'fontsize':12,})

4. 子圖間距調(diào)整、子圖邊距調(diào)整

  • subplots_adjust 調(diào)整間距。
  • 參數(shù) left=0,right=1,top=1,bottom=0, 分別控制 上下左右 的位置。(整體邊距)
  • 參數(shù) wspace=0.4,hspace=0.1 分別控制橫向和縱向的子圖間距。(子圖間距)
import matplotlib.pyplot as plt

fig,axes = plt.subplots(2,2,figsize=(8,6),dpi=100,facecolor="#00CC67")

# 遍歷所有子圖
for i,ax in enumerate(axes.flatten()):
    ax.text(0.5, 0.5, i, fontdict={'fontsize':20,})

# left 控制左邊位置;wspace,hspace 控制子圖間距
fig.subplots_adjust(left=0,right=1,top=1,bottom=0,
                    wspace=0.4,hspace=0.1)

總結(jié) 

到此這篇關(guān)于Python matplotlib設(shè)置多子圖、子圖間距及外邊距的幾種方式的文章就介紹到這了,更多相關(guān)matplotlib設(shè)置多子圖、子圖間距、外邊距內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論