For some reason in my subplot, the legend of the second ax is cropped at the bottom inside of the legend box. The legend box itself is perfectly fine. I’ve check if the grid or the tight layout were responsible but they weren’t. What could cause such an issue?
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
SMALL_SIZE = 15
MEDIUM_SIZE = 18
BIGGER_SIZE = 29
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=MEDIUM_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=MEDIUM_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
matplotlib.rcParams['mathtext.fontset'] = 'cm'
matplotlib.rcParams['font.family'] = 'STIXGeneral'
# Data
V_plus = np.array([92, 191, 283, 490, 660, 935, 1430, 2210, 2660, 3000]) * 1e-3
V_minus = np.array([97, 191, 285, 480, 665, 940, 1410, 2290, 2680, 3000]) * 1e-3
input_power = np.array([100, 201, 300, 503, 700, 990, 1500, 2500, 2980, 3500]) * 1e-3
# Linear regression
model_plus = LinearRegression()
r_plus = model_plus.fit(input_power.reshape(-1, 1), V_plus)
V_plus_pred = model_plus.predict(input_power.reshape(-1, 1))
model_minus = LinearRegression()
r_minus = model_minus.fit(input_power.reshape(-1, 1), V_minus)
V_minus_pred = model_minus.predict(input_power.reshape(-1, 1))
# Create figure and subplots
fig, ax = plt.subplots(1, 2, figsize=(15, 7))
ax1 = ax[0]
ax2 = ax[1]
# Plot for V_plus
ax1.scatter(input_power, V_plus, color='blue', label='Data')
ax1.plot(input_power, V_plus_pred, color='red', label=fr"""Linear fit: {model_plus.coef_[0]:.3f} $times V_+$""")
ax1.set_xlabel('Input Power (mW)')
ax1.set_ylabel('$V_+$ (V)')
ax1.set_title('$V_+$ vs Input Power')
ax1.grid(True)
ax1.legend(loc='lower right')
# Plot for V_minus
ax2.scatter(input_power, V_minus, color='green', label=r'Data')
ax2.plot(input_power, V_minus_pred, color='red', label=fr""""Linear fit: {model_minus.coef_[0]:.3f} $times V_-$""")
ax2.set_xlabel('Input Power (mW)')
ax2.set_ylabel('$V_{-}$ (V)')
ax2.set_title('$V_{-}$ vs Input Power')
ax2.grid(True)
ax2.legend(loc='lower right')
plt.tight_layout()
plt.show()