在 matplotlib 中优雅地组合两个图形
Combining two figures elegantly in matplotlib
您好,我在使用 matplotlib 显示然后整齐地保存 7 个子图时遇到了问题。该图由第一行饼图和第二行柱状图组成。
我已经设法让一些东西工作了,但是当我试图把所有的子图都放在一个图中时,我无法设法让一些东西达到良好的可读性。重要的是保存文件时能够输出一个包含所有组件的png文件。
第一个示例是 'correct',但在饼图和直方图之间生成宽 space:
这是一个代码 'example'(尽管不完全是我所拥有的,但这是我可以分享的最接近问题的工作示例):
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()
因此,我需要找到一种方法来减少 charts/histograms 之间的 space 或者将两个图形保存或拼接在一个文件中的方法。
有人有什么建议吗?
谢谢
使用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()
您好,我在使用 matplotlib 显示然后整齐地保存 7 个子图时遇到了问题。该图由第一行饼图和第二行柱状图组成。
我已经设法让一些东西工作了,但是当我试图把所有的子图都放在一个图中时,我无法设法让一些东西达到良好的可读性。重要的是保存文件时能够输出一个包含所有组件的png文件。
第一个示例是 'correct',但在饼图和直方图之间生成宽 space:
这是一个代码 'example'(尽管不完全是我所拥有的,但这是我可以分享的最接近问题的工作示例):
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()
因此,我需要找到一种方法来减少 charts/histograms 之间的 space 或者将两个图形保存或拼接在一个文件中的方法。
有人有什么建议吗?
谢谢
使用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()