Here we import data from a .csv file containing data from a CTD profiler (depth, temperature, salinity and fluorescence).
1. First, download the CTD_profile.csv file, and put it in your working directory.
2. Import file…
# Extract data from file ********************************* import numpy as np f = open('CTD_profile.csv', 'r') data = np.genfromtxt(f, delimiter=',') f.close()
3. Create variables
# Create variables with user-friendly names depth = data[:,0] temp = data[:,1] salt = data[:,2] fluo = data[:,3] del(data) # delete "data"... to keep things clean
4. Simple plot
# Plot data *********************************************** import matplotlib.pyplot as plt fig1 = plt.figure() ax1 = fig1.add_subplot(111) ax1.plot(temp,depth,'o-') # Draw x label ax1.set_xlabel('Temperature (C)') ax1.xaxis.set_label_position('top') # this moves the label to the top ax1.xaxis.set_ticks_position('top') # this moves the ticks to the top #ax1.xaxis.tick_top() # ANOTHER way to move the ticks to the top # Draw y label ax1.set_ylabel('Depth (m)') ax1.set_ylim(ax1.get_ylim()[::-1]) #this reverses the yaxis (i.e. deep at the bottom) plt.show()
5. More complex plot with subplots
# Three-panel plot fig2, (ax2, ax3, ax4) = plt.subplots(1,3,sharey=True) # Temperature ax2.plot(temp,depth,'o-') ax2.set_ylabel('Depth (m)') ax2.set_ylim(ax2.get_ylim()[::-1]) #this reverses the yaxis (i.e. deep at the bottom) ax2.set_xlabel('Temperature (C)') ax2.xaxis.set_label_position('top') # this moves the label to the top ax2.xaxis.set_ticks_position('top') # this moves the ticks to the top # Salinity ax3.plot(salt,depth,'o-r') ax3.set_xlabel('Salinity') ax3.xaxis.set_label_position('top') # this moves the label to the top ax3.xaxis.set_ticks_position('top') # this moves the ticks to the top ax3.yaxis.set_visible(False) # This erases the y ticks # Fluorescence ax4.plot(fluo,depth,'o-g') ax4.set_xlabel('Flourescence (V)') ax4.xaxis.set_label_position('top') # this moves the label to the top ax4.xaxis.set_ticks_position('top') # this moves the ticks to the top ax4.yaxis.set_visible(False) # This erases the y ticks
Pingback: T-S Diagram | Ocean Python
To do the equivalent but straight from a .cnv (i.e. the SeaBird file itself), one can use the PyCNV package (http://cnv.castelao.net) instead of steps 1-3:
pip install cnv
from cnv import fCNV
profile = fCNV(‘your_ctd_file.cnv’)
# To check which variables are available
profile.keys()
# For example, here you can access the depth as:
data[‘depth’]
# And the temperature as:
data[‘temperature’]
# And to access the date of the profile from the CTD header:
profile.attributes[‘datetime’]
I am very new to programming and am trying to modify what you have for the CTD plots so that they all plot in one pane with multiple x-axes. Any idea how one would go about doing this? Thank you so much!