Oceanographic plotting in Python
<p>I am plotting graphs. And I would like to have the range of values on the colorbars for graphs "U_velocity" and "U_shear_velocity" from -0.3 to 0.3. Moreover, I am trying to make the range of x ax in hours from 0 to 12.5 for U and V shear velocity plots but nothing works and instead of that I have meanings of the speed. How can I do that, please help me. </p>

<pre><code>from netCDF4 import *
import matplotlib as mp
import numpy as np

#import matplotlib.pyplot as plt
import pylab as plt


#%%

file = "/home/vlad/Desktop/task/Untitled Folder/result.nc" 


ncdata = Dataset(file, 'r')

u = np.squeeze(ncdata.variables['u'][:])
v = np.squeeze(ncdata.variables['v'][:])
z = np.squeeze(ncdata.variables['z'][:])

time = ncdata.variables['time'][:]/3600

ncdata.close()

u_mean = np.mean(u[0:100,:],0)
z_mean = np.mean(z[0:100,:],0)
v_mean = np.mean(v[0:100,:],0)

u_mean_10 = u[900:1000,:]
v_mean_10 = v[900:1000,:]
z_10 = np.mean(z[900:1000,:],0)

time_10 = time[900:1000] - time[900]


T = len(time_10)
L = len(z_10)

fig = plt.figure(6)
plt.pcolormesh(time_10,z_10,u_mean_10.T)
plt.xlim([0, time_10[-1]])
fig.suptitle('U_velocity', fontsize=25)
plt.xlabel('time', fontsize=20)
plt.ylabel('depth(m)', fontsize=20)
plt.colorbar()
plt.show()

shear_u_mean_10 = np.zeros([T,L])

for t in range(T):
    for i in range(L-1):
        tmp=(u_mean_10[t, i+1]-u_mean_10[t, i])/(z_10[i+1]-z_10[i])
        tmp_depth = 0.5 * (z_10[i+1]+z_10[i])
        shear_u_mean_10[t,i] = tmp

fig = plt.figure(10)
plt.pcolormesh(time_10/3600,z_10, shear_u_mean_10.T)
plt.xlim([0, time_10[-1]/3600])
plt.colorbar()
#plt.ylim([-30, -25])
fig.suptitle('U_shear velocity', fontsize=25)
plt.xlabel('time', fontsize=20)
plt.ylabel('depth(m)', fontsize=20)
plt.show()


shear_v_mean_10 = np.zeros([T,L])

for t in range(T):
    for i in range(L-1):
        tmp=(v_mean_10[t, i+1]-v_mean_10[t, i])/(z_10[i+1]-z_10[i])
        tmp_depth = 0.5 * (z_10[i+1]+z_10[i])
        shear_v_mean_10[t,i] = tmp

fig = plt.figure(11)
plt.pcolormesh(time_10/3600,z_10, shear_v_mean_10.T)
plt.xlim([0, time_10[-1]/3600])
plt.colorbar()
#plt.ylim([-30, -25])
fig.suptitle('V_shear velocity', fontsize=25)
plt.xlabel('time', fontsize=20)
plt.ylabel('depth(m)', fontsize=20)
plt.show()



fig = plt.figure(7)
plt.pcolormesh(time_10,z_10,v_mean_10.T)
plt.xlim([0, time_10[-1]])
fig.suptitle('V_velocity', fontsize=25)
plt.xlabel('time', fontsize=20)
plt.ylabel('depth(m)', fontsize=20)
plt.colorbar()
plt.show()
</code></pre>

<p>This is not an easy question to answer with a wall of code, reference to unknown file <code>result.nc</code> and several unrelated and fairly specific problems. The following may help:</p>

<p>The colorbar range can be set by passing <code>vmin=-0.3</code> and <code>vmax=0.3</code> to <code>pcolormesh</code>. </p>

<p>To limiting the range of time, you can use array slicing (e.g. <code>time[time&lt;12.5], u[time&lt;12.5]</code>). </p>

<p>For your data, surely the speed is just <code>speed = np.sqrt(np.power(u,2) + np.power(v,2)</code></p>

<p>Please provide a Minimal, Complete, and Verifiable example if you want further help.</p>

python python-2.7 python-3.x numpy matplotlib