Home > Blockchain >  How to increase plottable space above a subplot in matplotlib?
How to increase plottable space above a subplot in matplotlib?

Time:02-05

I am currently making a plot on matplotlib, which looks like below. enter image description here

The code for which is:

fig, ax1 = plt.subplots(figsize=(20,5))

ax2 = ax1.twinx()

# plt.subplots_adjust(top=1.4)

ax2.fill_between(dryhydro_df['Time'],dryhydro_df['Flow [m³/s]'],0,facecolor='lightgrey')
ax2.set_ylim([0,10])

AB = ax2.fill_between(dryhydro_df['Time'],[12]*len(dryhydro_df['Time']),9.25,facecolor=colors[0],alpha=0.5,clip_on=False)

ab = ax2.scatter(presence_df['Datetime'][presence_df['AB']==True],[9.5]*sum(presence_df['AB']==True),marker='X',color='black') 

# tidal heights
ax1.plot(tide_df['Time'],tide_df['Tide'],color='dimgrey')

I want the blue shaded region and black scatter to be above the plot. I can move the elements above the plot by using clip_on=False but I think I need to extend the space above the plot to do visualise it. Is there a way to do this? Mock-up of what I need is below:

enter image description here

CodePudding user response:

You can use clip_on=False to draw outside the main plot. To position the elements, an xaxis transform to put points

  • Related