How to reduce the gap between thin bars on x-axis using matplotlib.axes?

Viewed 44

This is the code that generates the graph so far: #Graph: numerical calculations y1,yerr1 = msc['mean'].to_numpy(),msc['sem'].to_numpy() x1=np.arange(len(y1)) #count the number of values in y that are not zero which will form the number of x-positions

    #Graph: Create
    fig, ax = plt.subplots(1,1, figsize = (4,6))
    ax.bar(x1, y1, color="#00004d",width=0.3, align='center')
    ax.errorbar(x1, y1, yerr=yerr1,fmt=' ', capsize=(3),zorder=0, ecolor='k')
    ax.spines['right'].set_visible(False), ax.spines['top'].set_visible(False)
    ax.set_xticks(x1)
    ax.set_xticklabels(u1), ax.set_ylabel(para.replace('_',' ')+'(g)'),ax.set_title(title) #labels
    ax.set_ylim(top=ymax)
    ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f')) #sets decimal places
    ax.locator_params(axis='y', nbins=5) #set the maximum number of ticks on the y-axis
    
    #save and export graph prior to significance line
    fig.savefig(para+"_JR.png",dpi=300)

And it produces this graph:

enter image description here

I've tried the following:

  • I've played around with the width and the large gap between the bars occurs the lower the width value is in line 2 of the code. If I increase the width size, then i'll get extremely thick bars but I would prefer to have thin bars in a graph.
  • I have also tried setting the figure size but then it cuts off parts of the ylabel when exporting as a png.
  • I have also tried removing the annotation and title of the graph but these had no effect.
  • I have looked into this question: stackoverflow.com/q/63209867/7758804 but it the code doesn't work for matplotlib.axes

I'm not sure what else to try to reduce the gap?

0 Answers
Related