0

I would like to plot two different parameters onto two x-axes (bottom and top) of one single plot and sharing the same y-axes.

I have been looking at other questions or tutorials: two (or more) graphs in one plot with different x-axis AND y-axis scales in python https://www.c-sharpcorner.com/article/twin-x-axis-graph-in-python/ but, because I am new to Python, I've just confused myself.

Here a data example: https://file.io/mNZoEE8rDZC0

and my code:

import numpy as np  
import matplotlib.pyplot as plt  
import pandas as pd

ctd = Data1
    
#let's start with stn8
st8 = ctd[ctd['Station']==8]

        
fig=plt.figure()
ax=fig.add_subplot(111, label="1")
ax2=fig.add_subplot(111, label="2", frame_on=False)

ax.scatter(st8['Var1'],st8['Depth'], color="C0")
ax.set_xlabel("Var1", color="C0")
ax.set_ylabel("Depth", color="C0")
ax.tick_params(axis='x', colors="C0")
plt.gca().invert_yaxis()

ax2.plot(st8['Var2'],st8['Depth'], color = 'C1')
ax2.xaxis.tick_top()
ax2.yaxis.tick_right()
ax2.set_xlabel('Var2', color="C1")
ax2.xaxis.set_label_position('top') 
ax2.yaxis.set_label_position('right')
ax2.tick_params(axis='x', colors="C1")


plt.show()

As you can notice, I am able to make a plot but the y-axes are not matching (I would like it increasing from top to bottom). Furthermore, I need to plot several stations (like 50). May you give me some advice on how to do it? I am not practical of for loops in Python

Strobila
  • 155
  • 2
  • 11

0 Answers0