Issue
As shown below, not sure whether there are too many signals in one subplot, hence the legend takes too much space, the plot itself is too small, i.e the height is short.
How may I make the plot bigger please?
Code for the plot
cm = 1/2.54
fig, axes = plt.subplots(nrows=len(unique_signals), ncols=1, figsize=(23.5*cm, 17.2*cm))
sig_col = filtered_df.columns[1:]
plot_counter = 0
previous_label = ""
for column in sig_col:
signal_name = column.split('_')[0] if ':' in column else column[:-1]
if signal_name != previous_label or plot_counter == 0:
ax = axes[plot_counter]
plot_counter += 1
ax.grid(True)
previous_label = signal_name
ax.plot(filtered_df['time'], filtered_df[column], label=column)
y_min, y_max = ax.get_ylim()
more_ext = ['Ilw1_X','Ilw2_X','IvwTrf1_X','IdcP_X','IdcN_X','Vlw2_X', 'Ilw1_Y','Ilw2_Y','IvwTrf1_Y','IdcP_Y','IdcN_Y','Vlw2_Y','Ivlv','IvlvSum','Icir','Ignd']
percentage = 0.02 if signal_name not in more_ext else 0.2
y_min_ext = y_min*(1-percentage) if y_min > 0 else y_min*(1+percentage)
y_max_ext = y_max*(1+percentage) if y_max > 0 else y_max*(1-percentage)
ax.set_ylim(y_min_ext, y_max_ext)
for ax in axes:
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.tight_layout()
plt.savefig(group_name.split('_')[0]+'.png', dpi=300)
plt.close()
Solution
I tend to take a more manual approach to plotting (for better or worse) especially when there are only a few subplots. For example:
import matplotlib.pyplot as pl
fig = pl.figure(figsize=(23.5*cm, 17.2*cm))
gs0 = fig.add_gridspec(4, 1) # define number of rows/cols of subplots
# changing "hspace" and "wspace" changes the spacing between subplots...which makes them bigger/smaller within your overall plot
gs0.update(left=0.025, right = 0.975,
top=0.975, bottom=0.025,
hspace=0.1, wspace=0.0)
# create subplot axes
ax0 = fig.add_subplot(gs0[0])
ax1 = fig.add_subplot(gs0[1])
ax2 = fig.add_subplot(gs0[2])
ax3 = fig.add_subplot(gs0[3])
# now plot your data
for column in sig_col:
signal_name = column.split('_')[0] if ':' in column else column[:-1]
if signal_name != previous_label or plot_counter == 0:
# NOTE CHANGE HERE
ax = fig.get_axes()[plot_counter]
plot_counter += 1
ax.grid(True)
# continue with your code
This code can be modified for more flexibility (i.e., changing the number of subplots) like so:
import matplotlib.pyplot as pl
fig = pl.figure(figsize=(23.5*cm, 17.2*cm))
gs0 = fig.add_gridspec(len(unique_signals), 1) # define number of rows/cols of subplots
# changing "hspace" and "wspace" changes the spacing between subplots...which
# makes them bigger/smaller within your overall plot
gs0.update(left=0.025, right = 0.975,
top=0.975, bottom=0.025,
hspace=0.1, wspace=0.0)
# now plot your data
for column in sig_col:
signal_name = column.split('_')[0] if ':' in column else column[:-1]
if signal_name != previous_label or plot_counter == 0:
# create axis on the fly
ax = fid.add_subplot(gs0[plot_counter])
plot_counter += 1
ax.grid(True)
# continue with your code
# finally, add your legend(s)
for axis_counter in range(len(unique_signals)):
ax = fig.get_axes()[axis_counter]
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
This approach gives you a lot of control over the individual subplots. If this does not resolve your issue my next recommendation is to increase the overall figure size, though I suspect you still need to address the inter-subplot spacing (which I do via gridspec).
Answered By - tnknepp
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.