Данные — это список из 12 фреймов данных, каждый из которых имеет 8 столбцов.
Код: Выделить всё
# Create a 4x3 grid of subplots
fig, axs = plt.subplots(4, 3, figsize=(11, 14))
# Flatten the 4x3 array to access each subplot individually
axs = axs.flatten()
for i, (df, ax) in enumerate(zip(Data, axs)):
x_values = [] # List to store x_col_values for current iteration
y_values = [] # List to store y_col_values for current iteration
for j in range(0, 7, 2):
x_col = df.columns[j]
x_col_values = np.array(df[x_col])
y_col = df.columns[j + 1]
y_col_values = np.array(df[y_col])
# Append current iteration's x_col_values and y_col_values to the lists
x_values.append(x_col_values)
y_values.append(y_col_values)
# Scatter plot for each pair of columns with a unique color
sc = ax.scatter(df[x_col], df[y_col], s=sizes[j], edgecolor='dark red',
linewidth=0.3, alpha=alphas[j], color=colors[j],
label=SS[j])
All_x = np.concatenate(x_values)
All_y = np.concatenate(y_values)
# Filter out the NaN values
All_x_without_nan = All_x[~(np.isnan(All_x))]
All_y_without_nan = All_y[~(np.isnan(All_y))]
# Reshape x to make it two-dimensional
x_new = All_x_without_nan[:, np.newaxis]
# Calculate the slope using lstsq when intercept is forced to zero
slope, _, _, _ = np.linalg.lstsq(x_new, All_y_without_nan, rcond=None)
# Calculate r_square from user defined function
r_squared = calculate_r_2(All_x_without_nan, All_y_without_nan)
ax.text(95, 1.8, f"Slope: {slope[0]:.2f}, $R^2$: {r_squared:.2f}",
fontsize = 8, color='black',
bbox= dict(facecolor='lightyellow', linewidth=0.5,
edgecolor='black', boxstyle='square, pad=0.4'))
ax.set_xlim(1, 15000)
ax.set_ylim(1,15000)
## Set x and y axes to log scale
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_aspect('equal')
minor_locator_x = LogLocator(base=10.0, subs=(0.2,0.3, 0.4, 0.5, 0.6,0.7, 0.8, 0.9), numticks=10)
minor_locator_y = LogLocator(base=10.0, subs=(0.2,0.3, 0.4, 0.5, 0.6,0.7, 0.8, 0.9), numticks=10)
ax.xaxis.set_minor_locator(minor_locator_x)
ax.yaxis.set_minor_locator(minor_locator_y)
# Add plot number within the plot
ax.text(0.05, 0.9, f'Plot {i+1}', transform=ax.transAxes, fontsize=10, fontweight='bold', color='black')
plt.savefig('Test.png', dpi = 800)
Подробнее здесь: https://stackoverflow.com/questions/782 ... round-text