'How can I create a sub plot in Matplotlib with one of the subplots showing xaxis values on y-ticks?

I am trying to create a subplot similar to the one shown below. I just sketched it up to give an idea of what kind of plot I am trying to build. Also, I created some dataset (see below) to reproduce my real dataframe. As you can see, it would have been much easier for me to build the subplots if there was no depth axis. The depth column is actually an xaxis, but I don't want to make a scatter plot (date vs depth) like other subplots, but instead I want to plot it in the same way as in the sample plot on the yaxis ticks. The date column should be shared among all subplots, so I can zoom in/out all the plots simultaneously. I am able to create simple subplots, but I couldn't get my head around how to add the depth column as shown here.

enter image description here

 date   depth   temp    pressure
01-01-22    1200    50  1200
02-01-22    1250    52  1250
03-01-22    1350    55  1300
04-01-22    1355    59  1220
05-01-22    1360    56  1200
06-01-22    1370    52  1230

additional info - I am using this code, and please see the plot below. It seems ok on the plot, but it is actually not doing what I needed. To show why it is not doing what I need, I plotted Datetime vs Depth (similar plot) on the next axis. As you can see, for instance, the depth 1000m should appear at a different Datetime value, whereas on Depth yticks 1000m appears at a later date. I think this Depth values on 1st axis yticks are evenly distributed. You can also see on the sketch, the depth values are not evenly distributed. I can't seem to figure out how to get it working the way I need it?

    fig, axes = plt.subplots(figsize=(10,10), sharey=True)
    
    gs = gridspec.GridSpec(1, 3, width_ratios=[5,5,5])
    
    ax1 = plt.subplot(gs[0])
    ax2 = plt.subplot(gs[1])
    ax3 = plt.subplot(gs[1])
    ax4 = plt.subplot(gs[1])
    ax5 = plt.subplot(gs[2])
    
    ax1.get_shared_y_axes().join(ax1,ax2,ax3,ax4,ax5)
    
    ax1.xaxis.set_visible(False)
        
    ax1.set_ylim(df['DATE_TIME'].min(), df['DATE_TIME'].max())
    ax1.annotate('Datetime', xy=(0, 1.02), xycoords="axes fraction", fontsize=12, ha="center") 
fraction", fontsize="large", ha="center") 
    
    ax3 = ax1.twinx()
    spine_distance = 0.7
    ax3.spines["right"].set_position(("axes", spine_distance)) 
    ax3.tick_params(axis="y", direction="in", pad=-30)
    ax3.set_ylim(df['Depth (m)'].min(), df['Depth (m)'].max())
    ax3.annotate('Depth', xy=(spine_distance, 1.02), `xycoords="axes,fraction", fontsize="large", ha="center")`
    
    ax4.plot(df['Depth (m)'],df['DATE_TIME'], color='black', linewidth=0.9)
    ax4.annotate('Depth m', xy=(spine_distance, 1.02), xycoords="axes fraction", fontsize="large", ha="center")
    
    ax5.plot(df['temp'],df['DATE_TIME'], color='blue', linewidth=0.9)
    ax5.annotate('Temp', xy=(spine_distance, 1.02), xycoords="axes fraction", fontsize="large", ha="center")
    
    for ax in [ax4,ax5]:
        plt.setp(ax.get_yticklabels(), visible=False)
    plt.show()

enter image description here

enter image description here

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source