How to put the legend outside the plot
#11
I've tested the last code you provided and adapted it to my project. It works as I required. For anyone interested, here's the final working code:

Code:
n_plots = 20
cols = 5
rows = n_plots // cols + (n_plots % cols > 0)
fig = plt.figure(figsize = (cols * 4, rows * 4)) # Define figure size based on number of columns and row
for i in range(n_plots):
    ax = fig.add_subplot(rows, cols, i + 1)
ax.plot([1, 2, 3], [i + 1, i + 2, i * 0.5], label = f 'Line {i}')
ax.legend(loc = 'center left', bbox_to_anchor = (1, 0.5))
ax.set_title(f 'Plot {i+1}')
plt.tight_layout()
plt.subplots_adjust(right = 0.9) # Adjust the right side to accommodate legends
plt.show()
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)