python 图例元素多导致情节小

wgx48brx  于 2023-10-15  发布在  Python
关注(0)|答案(1)|浏览(85)

如下图所示,不确定一个子图中是否有太多信号,因此图例占用太多空间,图本身太小,即高度短。
请问我怎样才能使情节更大?
图的代码

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()

我的期望:

2wnc66cl

2wnc66cl1#

我倾向于采取更手动的方法来绘制(无论好坏),特别是当只有几个子情节时。举例来说:

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

可以修改此代码以获得更大的灵活性(即,更改子图的数量),如下所示:

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))

这种方法使您可以对各个子情节进行大量控制。如果这还不能解决你的问题,我的下一个建议是增加整个图的大小,尽管我怀疑你仍然需要解决子图间的间距(我通过gridspec)。

相关问题