matplotlib.pyplot.subplots中没有轮廓图的inline_spacing

0 投票
1 回答
966 浏览
提问于 2025-04-18 16:18

我正在使用 matplotlib.pyplot.subplots 来创建一个 3x3 的图表,因为这样可以方便地共享坐标轴。然后我在每个子图中绘制了等高线填充图和等高线图。当我在等高线图中放置文本时,只有最后一个图的行间距是正确的,其余的图都没有行间距。就好像我没有为前面 8 个子图开启行内文本一样。以下是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import rfFile, rfMath, rfGeneral
plt.ion()

de_1 = list(np.linspace(0,100,101))
de_2 = list(np.linspace(0,100,101))
gain_2 = np.linspace(0,16,9)
levels = np.linspace(0,100,11)

de = np.array([[x,y] for x in de_1 for y in de_2])
dx = de[:,0]
dy = de[:,1]
xytri = rfGeneral.createTriMesh(dx, dy, 2)

fig, ax = plt.subplots(nrows=3, ncols=3, sharex=True, sharey=True)
ax = ax.flatten()
for ix, g in enumerate(gain_2):
    print g
    de_tot = [(x*y)/(x + y/rfMath.db2lin(g)) for x in de_1 for y in de_2]
    de_tot = np.nan_to_num(de_tot)
    cs = ax[ix].tricontourf(xytri, de_tot, levels)
    ax[ix].set_title('Gain: {:2.0f} dB'.format(g))
    cs1 = ax[ix].tricontour(xytri, de_tot, levels, linewidths=1.5, colors='k')
    ax[ix].clabel(cs1, fmt = '%2.0f', fontsize=14, inline=1)

ax[0].set_ylim(min(de_2),max(de_2))
ax[0].set_xlim(min(de_1),max(de_1))
cax = plt.axes([0.93, 0.1, 0.025, 0.8])
fig.colorbar(cs, cax=cax)
ax[7].set_xlabel(r'$\eta_{D1}\,[\%]$',fontdict={'fontsize':20})
ax[3].set_ylabel(r'$\eta_{D2}\,[\%]$',fontdict={'fontsize':20})
fig.suptitle('Total Drain Efficiency', fontsize=24)
plt.subplots_adjust(top=0.9, left=0.075, right=0.9) 

1 个回答

0

如果你能提供一个简单的例子,让大家能立刻复现你遇到的问题,那会很有帮助。rf*模块(这是什么?)让别人无法运行你的代码,只会让问题的原因变得更加模糊。

不过,把 sharexsharey 设置为 False 可能会解决你的问题。你可以根据数据手动设置 x 和 y 的范围。

我不太确定是什么导致了这个问题。也许共享坐标轴会对 clip_path 进行某种变换,这样只对最后一个坐标轴有效。在我看来,这看起来像是一个bug。

使用共享坐标轴时:

import numpy as np
import matplotlib.pyplot as plt

X, Y = np.meshgrid(np.arange(-3.0, 3.0, 0.025), np.arange(-3.0, 3.0, 0.025))

fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(8,4), sharex=True, sharey=True,
                        subplot_kw={'xticks': [], 'yticks': []})

fig.subplots_adjust(hspace=0.05, wspace=0.05)

for ax in axs.flat:
    cs = ax.contour(X, Y, X+Y)
    ax.clabel(cs, inline=1, fontsize=10)

在这里输入图片描述

不使用共享坐标轴时:

import numpy as np
import matplotlib.pyplot as plt

X, Y = np.meshgrid(np.arange(-3.0, 3.0, 0.025), np.arange(-3.0, 3.0, 0.025))

fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(8,4), sharex=False, sharey=False,
                        subplot_kw={'xticks': [], 'yticks': []})

fig.subplots_adjust(hspace=0.05, wspace=0.05)

for ax in axs.flat:
    cs = ax.contour(X, Y, X+Y)
    ax.clabel(cs, inline=1, fontsize=10)

在这里输入图片描述

撰写回答