Skip to main content
 首页 » 编程设计

python之Matplotlib 图放置

2025年05月04日19开发

我需要一些格式方面的帮助。如何更改第二个和第三个图形之间的距离

   from matplotlib.font_manager import FontProperties 
   import matplotlib.pyplot as plt 
 
 
   fig  = plt.figure(1) 
   graph1 = fig.add_subplot(3,1,1) 
   graph2 = fig.add_subplot(3,1,2) 
   norm =  fig.add_subplot(3,1,3) 
 
   graph1.set_title(TITLE + '\nscaling factor: ' +str(round(rescale,3))) 
   norm.set_title('Circle and Oval Height Difference') 
   norm.set_xlabel(XLABEL +'(Degrees)') 
   norm.legend(bbox_to_anchor=(1.13,1), prop={'size':8}) 
   plt.ylabel('Heights (nm)') 
 
 
   graph1.legend(bbox_to_anchor=(1.13,1),prop={'size':8}) 
   graph2.legend(bbox_to_anchor=(1.13,1),prop={'size':8}) 
   fontP = FontProperties() 
   fontP.set_size('small') 

请您参考如下方法:

快速回答是fig.subplots_adjust , 将 hspace 设置为更大的值。不幸的是,这还在前两个图表之间插入了一些空间,但这无论如何都可能很好,具体取决于您想要什么。正如 Joe 所说,如果 x 轴相同,我通常会删除它们。

如果您希望间距不均匀,即 2 和 3 之间的间距大于 1 和 2 之间的间距,您需要使用 fig.add_axes 在特定位置显式实例化轴.