使用相同的x轴和子图时重叠图[重复]

问题描述 投票:0回答:1

我无法使用对数刻度来插值数据,我找到的唯一解决方案是在同一图中绘制插值函数和图形,但使用不同的 xscale。它工作正常,但是当在右侧添加另一个图(子图)时,ylabel 与“主”图重叠。有什么我可以做的吗? 这是代码:

# Grafico di BOde e frequenza di taglio
popt, pcov = curve_fit(Lin, LOGf_der[0: 4], A_der[0: 4])

x = np.linspace(2, 4, 100)
RESIDUI = Residui(LOGf_der[0:4], A_der[0:4], popt[0], popt[1])
xRES = np.linspace(LOGf_der[0], LOGf_der[4], len(RESIDUI))

fig, axs = plt.subplots(1, 2, figsize=(20, 10))

ax = axs[0]

ax.scatter(f_der, A_der)
ax.tick_params(axis="both", which="major", labelsize=15)
ax.set_xscale('log')
ax.legend(loc="upper left", fontsize=16)
ax.grid(True, which='both')
ax.set_xlabel("Frequenza Hz", fontsize = 16)
ax.set_ylabel("Guadagno A", fontsize = 16)
ax.set_title("Grafico di Bode", fontsize = 18)

ax = ax.twiny()

ax.plot(LOGf_der, A_der, '--o', label="Dati sperimentali")
ax.plot(LOGf_VTC, A_VTC, label="VTC", c = 'purple')
ax.plot(x, Lin(x, popt[0], popt[1]), color="red", lw=1.4, label="Interpolazione lineare")
ax.tick_params(axis="both", which="major", labelsize=15)
ax.axis('off')
ax.legend(fontsize = 16)

fig.tight_layout()

ax = axs[1]

ax.scatter(xRES, RESIDUI, s = 80, label= ("Residui"))
ax.hlines(0, 1.9, 4.3, color="orange", linestyle="dashed", lw = 3, label=(r"$f(x) = 0$"))
ax.grid(True)
ax.set_xlabel(r"$\log_{10} f, Hz$", fontsize=16)
ax.set_ylabel("Residui", fontsize=16)
ax.set_title("Grafico dei residui", fontsize=18)
ax.tick_params(axis="both", which="major", labelsize=15)
ax.legend(fontsize = 16)

这就是我得到的。我怎样才能解决这个问题?谢谢!

python matplotlib subplot figure
1个回答
0
投票

希望你一切都好!感谢您提供绘图代码 - 重叠的标签绝对会令人沮丧。

看起来第二个子图上的 ylabel 换成了两行,占用了更多的垂直空间。不过没问题,我们可以通过一些调整来解决这个问题。

关键是使用 subplots_adjust 在子图之间添加一些填充。创建图形后尝试添加此行:

fig, axs = plt.subplots(2) 

fig.subplots_adjust(hspace=0.5) 

这应该创造一些喘息空间并防止 ylabel 重叠。您可以随时向上或向下调整

hspace
值,直到找到合适的平衡。

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# Define your functions Lin and Residui here

# Your data and curve fitting code

fig, axs = plt.subplots(1, 2, figsize=(20, 10)

# Adjust the horizontal space between the subplots
plt.subplots_adjust(wspace=0.3)

ax = axs[0]

# Your plotting code for the first subplot

ax = axs[1]

# Your plotting code for the second subplot

plt.show()

如果这有效或者您有任何其他问题,请告诉我!很好地格式化绘图需要一些技巧。但采取一些小步骤(例如添加一些填充物)可以大有帮助。希望这有助于解决您的标签问题 - 如果出现任何其他问题,请随时与我们联系。

© www.soinside.com 2019 - 2024. All rights reserved.