Matplotlib和使用极角投影移除子图之间的空白

时间:2018-11-05 06:17:18

标签: python matplotlib

接着回答问题How to remove the space between subplots in matplotlib.pyplot?,在多次尝试创建子图之间没有间隔之后,我发现所有解决方案均不适用于极投影。如果删除了极坐标投影(即并排显示没有空白的子图),但是下面的两个代码示例都可以正常工作,但是当添加极坐标时,列之间将显示空白。

样本1

m,n,_ = ws.shape
fig,axs = plt.subplots(m,n,
                       subplot_kw=dict(projection='polar'),
                       gridspec_kw={'wspace':0,'hspace':0})

for i in range(m):
    for j in range(n):
        ax = axs[i][j] # current axes
        w = ws[i][j]   # current weight vector
        a = numpy.linspace(0,2*numpy.pi,len(w),endpoint=False)
        a = numpy.concatenate((a,[a[0]]))
        w = numpy.concatenate((w,[w[0]]))

        ax.fill(a,w,alpha=1.0)
        ax.set_thetagrids([])
        ax.set_yticklabels([])
        ax.set_rticks([])
plt.show()  

示例2

m,n,_ = ws.shape
fig = plt.figure()
gs = gridspec.GridSpec(m,n,width_ratios=[1,1,1,1,1,1],wspace=0.0,hspace=0.0)
for i in range(m):
    for j in range(n):
        ax = plt.subplot(gs[i,j],projection='polar')

        w = ws[i][j]   # current weight vector
        a = numpy.linspace(0,2*numpy.pi,len(w),endpoint=False)
        a = numpy.concatenate((a,[a[0]]))
        w = numpy.concatenate((w,[w[0]]))

        ax.fill(a,w,alpha=1.0)
        ax.set_thetagrids([])
        ax.set_yticklabels([])
        ax.set_rticks([])            
plt.show()

我也根据其他答案中的建议在matplotlibrc中修改了参数。有人对此提出解决方案吗?

1 个答案:

答案 0 :(得分:0)

与您链接的答案类似,执行极坐标图会自动将轴的纵横比设置为“相等”,以得到美观的圆(而不是椭圆形)。这意味着,如果希望轴触摸,则还需要正确调整图形尺寸,以使其具有正确的宽高比。

m,n = 7,3
fig,axs = plt.subplots(m,n,
                       subplot_kw=dict(projection='polar'),
                       gridspec_kw={'wspace':0,'hspace':0,
                                    'top':1., 'bottom':0., 'left':0., 'right':1.},
                       figsize=(n,m))  # <- adjust figsize but keep ratio n/m
for ax in axs.flat:
    ax.set_thetagrids([])
    ax.set_yticklabels([])
    ax.set_rticks([])
plt.show()

enter image description here