嗨,我有一個問題,需要顯示然后使用 matplotlib 整齊地保存 7 個子圖。該圖由第一行的餅圖和第二行的直方圖組成。
我已經設法使某些東西正常作業,但是在嘗試將所有子圖集中在一個圖中時,我無法設法使某些東西具有良好的可讀性。重要的是在保存檔案時能夠輸出一個包含所有組件的png檔案。
第一個例子是“正確的”,但在餅圖和直方圖之間產生了很大的空間:

這是一個代碼“示例”(盡管不完全是我所擁有的,但這是與我可以分享的問題最接近的作業示例):
histo_1 = np.random.choice(500, 100, replace=True)
histo_2 = np.random.choice(250, 7000, replace=True)
histo_3 = np.random.choice(150, 1500, replace=True)
histo_4 = np.random.choice(2000, 250, replace=True)
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
f = plt.figure(figsize = (14, 15))
sn.set()
sn.color_palette("hls", 8)
ax = f.add_subplot(2,4,1)
plt.pie(sizes, labels = labels, autopct='%1.1f%%', textprops={'fontsize': 13})
plt.title("Distribution")
ax = f.add_subplot(2,4,2)
plt.pie(sizes, labels = labels, autopct='%1.1f%%', textprops={'fontsize': 13})
plt.title("Distribution")
ax = f.add_subplot(2,4,3)
plt.pie(sizes, labels = labels, autopct='%1.1f%%', textprops={'fontsize': 13})
plt.title("Distribution")
ax = f.add_subplot(2,4,5)
plt.hist(histo_1, bins='fd', color = "cornflowerblue", edgecolor='white', linewidth=1.2)
plt.title("Distribution 1")
ax1 = f.add_subplot(2,4,6)
plt.hist(histo_2, bins='fd', color = "indianred", edgecolor='white', linewidth=1.2)
plt.title("Distribution 2")
ax2 = f.add_subplot(2,4,7)
plt.hist(histo_3, bins='fd', color = "seagreen", edgecolor='white', linewidth=1.2)
plt.title("Distribution 3")
ax3 = f.add_subplot(2,4,8)
plt.hist(histo_3, bins='fd', color = "peru", edgecolor='white', linewidth=1.2)
plt.title("Distribution 4")
plt.tight_layout()
plt.show()
讓它看起來不錯的唯一方法是有兩個分開的數字,例如:

histo_1 = np.random.choice(500, 100, replace=True)
histo_2 = np.random.choice(250, 7000, replace=True)
histo_3 = np.random.choice(150, 1500, replace=True)
histo_4 = np.random.choice(2000, 250, replace=True)
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
f = plt.figure(figsize = (14, 15))
sn.set()
sn.color_palette("hls", 8)
ax = f.add_subplot(1,3,1)
plt.pie(sizes, labels = labels, autopct='%1.1f%%', textprops={'fontsize': 13})
plt.title("Distribution")
ax = f.add_subplot(1,3,2)
plt.pie(sizes, labels = labels, autopct='%1.1f%%', textprops={'fontsize': 13})
plt.title("Distribution")
ax = f.add_subplot(1,3,3)
plt.pie(sizes, labels = labels, autopct='%1.1f%%', textprops={'fontsize': 13})
plt.title("Distribution")
plt.tight_layout()
plt.show()
f = plt.figure(figsize = (14, 8))
ax = f.add_subplot(1,4,1)
plt.hist(histo_1, bins='fd', color = "cornflowerblue", edgecolor='white', linewidth=1.2)
plt.title("Distribution 1")
ax1 = f.add_subplot(1,4,2)
plt.hist(histo_2, bins='fd', color = "indianred", edgecolor='white', linewidth=1.2)
plt.title("Distribution 2")
ax2 = f.add_subplot(1,4,3)
plt.hist(histo_3, bins='fd', color = "seagreen", edgecolor='white', linewidth=1.2)
plt.title("Distribution 3")
ax3 = f.add_subplot(1,4,4)
plt.hist(histo_3, bins='fd', color = "peru", edgecolor='white', linewidth=1.2)
plt.title("Distribution 4")
plt.tight_layout()
plt.show()
因此,我需要找到一種方法來減少圖表/直方圖之間的空間,或者一種將兩個圖形保存或拼接到一個檔案中的方法。
有人有建議嗎?
謝謝
uj5u.com熱心網友回復:
使用 Gridspec,我設定了一個兩行四列的子圖,調整了子圖之間的高度,并將子圖的整體高度和底部調整為它們的值。這會將保存的影像保留為一張,并且我在保存引數中省略了更多邊距。
import matplotlib.pyplot as plt
import seaborn as sn
from matplotlib.gridspec import GridSpec
histo_1 = np.random.choice(500, 100, replace=True)
histo_2 = np.random.choice(250, 7000, replace=True)
histo_3 = np.random.choice(150, 1500, replace=True)
histo_4 = np.random.choice(2000, 250, replace=True)
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
fig = plt.figure(figsize = (14, 14))# ,constrained_layout=True
sn.set()
sn.color_palette("hls", 8)
gs = GridSpec(nrows=2, ncols=4, hspace=0, figure=fig) #height_ratios=[1.0, 0.40],
fig.subplots_adjust(top=0.65, bottom=0.25, hspace=0.0)
ax1 = fig.add_subplot(gs[0,0])
ax1.pie(sizes, labels = labels, autopct='%1.1f%%', textprops={'fontsize': 13})
ax1.set_title("Distribution")
ax2 = fig.add_subplot(gs[0,1])
ax2.pie(sizes, labels = labels, autopct='%1.1f%%', textprops={'fontsize': 13})
ax2.set_title("Distribution")
ax3 = fig.add_subplot(gs[0,2])
ax3.pie(sizes, labels = labels, autopct='%1.1f%%', textprops={'fontsize': 13})
ax3.set_title("Distribution")
ax4 = fig.add_subplot(gs[1,0])
ax4.hist(histo_1, bins='fd', color = "cornflowerblue", edgecolor='white', linewidth=1.2)
ax4.set_title("Distribution 1")
ax5 = fig.add_subplot(gs[1,1])
ax5.hist(histo_2, bins='fd', color = "indianred", edgecolor='white', linewidth=1.2)
ax5.set_title("Distribution 2")
ax6 = fig.add_subplot(gs[1,2])
ax6.hist(histo_3, bins='fd', color = "seagreen", edgecolor='white', linewidth=1.2)
ax6.set_title("Distribution 3")
ax6 = fig.add_subplot(gs[1,3])
ax6.hist(histo_3, bins='fd', color = "peru", edgecolor='white', linewidth=1.2)
ax6.set_title("Distribution 4")
#plt.tight_layout()
plt.savefig('./data/combine_plot.png', bbox_inches='tight')
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/317818.html
標籤:Python matplotlib 直方图 饼形图
