一、Matplotlib子圖間距離
Matplotlib是Python的一個(gè)強(qiáng)大的數(shù)據(jù)可視化工具,子圖可以有效地幫助人們理解數(shù)據(jù)。但是,Matplotlib子圖之間的距離可能會(huì)影響到整個(gè)圖表的展示效果。因此,我們需要在圖形輸出前對(duì)子圖之間的間距進(jìn)行調(diào)整。
Matplotlib子圖間距主要通過(guò)調(diào)整Figure和Subplot參數(shù),例如subplot間距,左邊距、右邊距、上邊距、下邊距等。
以下是一個(gè)簡(jiǎn)單的示例:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(7,7))
ax1 = fig.add_subplot(211)
ax1.plot([1,2,3], [4,5,6], 'o-', label="Line 1")
ax2 = fig.add_subplot(212)
ax2.plot([1,2,3],[2,3,4],'o-', label="Line 2")
plt.show()
輸出:
二、Matplotlib調(diào)整子圖間距
我們可以使用subplots_adjust()函數(shù)來(lái)調(diào)整子圖之間的間距。這個(gè)函數(shù)的作用是設(shè)置子圖周圍的空白區(qū)域的大小。
以下是一個(gè)簡(jiǎn)單的示例:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(8, 4))
for i in range(2):
ax = axes[i]
x = range(10)
y = [j*(i+1) for j in x]
ax.plot(x, y)
ax.set_title('Subplot ' + str(i+1), fontsize=10)
plt.subplots_adjust(hspace=0.5)
plt.show()
輸出:
三、Matplotlib刻度間距
在Matplotlib中,我們可以使用xticks()函數(shù)和yticks()函數(shù)來(lái)設(shè)置坐標(biāo)軸刻度標(biāo)簽,例如刻度標(biāo)簽格式、旋轉(zhuǎn)角度和間距等。
以下是一個(gè)簡(jiǎn)單的示例:
import matplotlib.pyplot as plt
import numpy as np
n = 5
x = np.arange(n)
y1 = (1-x/float(n)) * np.random.uniform(0.5,1.0,n)
y2 = (1-x/float(n)) * np.random.uniform(0.5,1.0,n)
fig, ax = plt.subplots()
ax.plot(x, y1, 'o-', label='Line 1')
ax.plot(x, y2, 'o-', label='Line 2')
ax.xaxis.set_ticks(np.arange(0, n, 1))
ax.xaxis.set_ticklabels(['One', 'Two', 'Three', 'Four', 'Five'], fontsize=10, rotation=30)
plt.legend(loc='best')
plt.show()
輸出:
四、Matplotlib子圖大小
我們可以使用figsize參數(shù)來(lái)調(diào)整子圖的大小,使其更適合不同的輸出需求。
以下是一個(gè)簡(jiǎn)單的示例:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10, 5))
ax1 = fig.add_subplot(121)
ax1.plot([1,2,3], [4,5,6], 'o-', label="Line 1")
ax1.set_title('Subplot 1', fontsize=12)
ax2 = fig.add_subplot(122)
ax2.plot([1,2,3],[2,3,4],'o-', label="Line 2")
ax2.set_title('Subplot 2', fontsize=12)
plt.show()
輸出:
五、Matplotlib子圖標(biāo)題
我們可以使用set_title()函數(shù)為子圖添加標(biāo)題。這是一個(gè)非常重要的功能,它可以讓觀察者更好地理解子圖的內(nèi)容。
以下是一個(gè)簡(jiǎn)單的示例:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(8, 4))
for i in range(2):
ax = axes[i]
x = range(10)
y = [j*(i+1) for j in x]
ax.plot(x, y)
ax.set_title('Subplot ' + str(i+1), fontsize=12)
plt.show()
輸出:
六、Matplotlib畫子圖
當(dāng)我們需要在一個(gè)圖表中畫多個(gè)子圖的時(shí)候,我們可以使用subplots()函數(shù)來(lái)創(chuàng)建多個(gè)子圖。
以下是一個(gè)簡(jiǎn)單的示例:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 6))
for i in range(2):
for j in range(2):
ax = axes[i][j]
x = range(10)
y = [j*(i+1) for j in x]
ax.plot(x, y)
ax.set_title('Subplot ' + str(i*2+j+1), fontsize=12)
plt.tight_layout()
plt.show()
輸出:
七、Matplotlib更改橫軸間距
我們可以使用set_xticks()函數(shù)和set_xticklabels()函數(shù)來(lái)更改橫軸的間距。
以下是一個(gè)簡(jiǎn)單的示例:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 5, 0.1)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xticks(np.arange(0, 5, 1))
ax.set_xticklabels(['0', '1', '2', '3', '4'], fontsize=10)
plt.show()
輸出:
八、Matplotlib創(chuàng)建子圖
我們可以使用add_subplot()函數(shù)來(lái)創(chuàng)建一個(gè)或多個(gè)子圖。
以下是一個(gè)簡(jiǎn)單的示例:
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax1.plot([1,2,3], [4,5,6], 'o-', label="Line 1")
ax1.set_title('Subplot 1', fontsize=12)
ax2 = fig.add_subplot(222)
ax2.plot([1,2,3],[2,3,4],'o-', label="Line 2")
ax2.set_title('Subplot 2', fontsize=12)
ax3 = fig.add_subplot(223)
ax3.plot([1,2,3],[4,6,8],'o-', label="Line 3")
ax3.set_title('Subplot 3', fontsize=12)
ax4 = fig.add_subplot(224)
ax4.plot([1,2,3],[1,3,5],'o-', label="Line 4")
ax4.set_title('Subplot 4', fontsize=12)
plt.show()
輸出: