Ytick overlaps with Matplotlib figure space in violinplot

41 Views Asked by At

The violin plots I am making with Matplotlib have one of the plot intersecting with ytick label. See the picture below:

The code I am using as follow:


    alp = 1
    fig, axes = plt.subplots(nrows=10, ncols=1, figsize=(8, 6), dpi=100)#, sharex=True)
    for i,t in enumerate(top_features):
      # print(i)
      alp = alp - 0.08
      parts = axes[i].violinplot(data[i], vert=False, widths=0.9,
                          showmeans=True, showextrema=True, showmedians=False,
                          bw_method='scott')#, points=200)
      for pc in parts['bodies']:
        pc.set_facecolor('#D43F3A')
        pc.set_edgecolor('black')
        pc.set_alpha(alp)
      axes[i].set_ylabel('{:.3f}'.format(top_features[i]), rotation=0)
    
      # axes[i].axis('off')
      axes[i].set_xlim(-0.0010, 0.0030)
      axes[i].spines['top'].set_visible(False)
      axes[i].spines['right'].set_visible(False)
      axes[i].spines['bottom'].set_visible(False)
      axes[i].spines['left'].set_visible(False)
    
    for i, ax in enumerate(axes):
        if i !=9:
          ax.set_xticks([])
        ax.set_yticks([])
    fig.tight_layout()
    fig.subplots_adjust(left=0.01,right= 1.1, hspace=0.14, wspace=0.3)
    ax.set_yticklabels(top_features)

How can move the figure to the right with the shared X-axis ticks and all the other violinplots?

1

There are 1 best solutions below

0
On

That moment when you find an answer to your own question! -_-

axes[i].set_ylabel('{:.3f}'.format(top_features[i]), rotation=0, labelpad=30)

Adjusting the labelpad value solves!

Thanks all!