Wind Circulation
Wind Circulation

Reputation: 107

How to add colorbars and Titles to the subplots?

How to add colorbars and titles to each each and every plot by using for command just like this picture reference_figure

import numpy as np
import matplotlib.pyplot as plt
import wradlib as wrl
import pyart
import xarray as xr

ds = xr.open_dataset('MDV-20180602-101927-PPIVol.nc')

def sweep(i):
    si = ds.sweep_start_ray_index.values
    ei = ds.sweep_end_ray_index.values
    return slice(si[i],ei[i]+1)

i=0
x = ds.range * np.sin(np.deg2rad(ds.azimuth[sweep(i)]))
y = ds.range * np.cos(np.deg2rad(ds.azimuth[sweep(i)]))

fig, ax = plt.subplots(3, 2, figsize=(12, 15))
ax = ax.flatten()

variables = ['reflectivity',"velocity",'zdr','rhohv','phidp','spectral width']

ref = ax[0].pcolormesh(x/1e3, y/1e3, ds['DBZH'][sweep(i)].T, cmap='pyart_NWSRef')
vel = ax[1].pcolormesh(x/1e3, y/1e3, ds['VELH'][sweep(i)].T, cmap='pyart_NWSVel')
zdr = ax[2].pcolormesh(x/1e3, y/1e3, ds['ZDR'][sweep(i)].T, cmap='pyart_RefDiff', vmin=-1, vmax=8)
rho = ax[3].pcolormesh(x/1e3, y/1e3, ds['RHOHV'][sweep(i)].T, cmap='pyart_RefDiff', vmin=0.5, vmax=1.05)
phidp = ax[4].pcolormesh(x/1e3, y/1e3, ds['PHIDP'][sweep(i)].T, cmap='pyart_Wild25', vmin=-180, vmax=180)
width = ax[5].pcolormesh(x/1e3, y/1e3, ds['WIDTHH'][sweep(i)].T, cmap='pyart_NWS_SPW')

for myax in ax:
    [myax.plot(k * np.cos(ds.azimuth[sweep(i)] * np.pi / 180),
              k * np.sin(ds.azimuth[sweep(i)] * np.pi / 180), 'k-', linewidth=1, alpha=0.5) for k in [25,75,125]]
    myax.set_aspect('equal')

fig.tight_layout()
plt.show()

and the output is Output

the variables are the titles which should be used

Upvotes: 1

Views: 380

Answers (1)

BenT
BenT

Reputation: 3200

To get the titles you want you just need to iterate through the list within the for loop. So it should look like below for just the variable name. You can do the same thing specifying your other axis labels. Colorbars will work the same way but you will need to use the name for each of your plots.

variables = ['reflectivity',"velocity",'zdr','rhohv','phidp','spectral width']

subplot_= [ref, vel, zdr, rho, phidp, width)

index=0

for myax in ax:
    [myax.plot(k * np.cos(ds.azimuth[sweep(i)] * np.pi / 180),
              k * np.sin(ds.azimuth[sweep(i)] * np.pi / 180), 'k-', linewidth=1, alpha=0.5) for k in [25,75,125]]
    myax.set_aspect('equal')

    myax.set_title(variables[index]) #Create a title on the axis

    plt.colorbar(subplot_[index],ax=myax)

    index+=1 #Add 1 

Upvotes: 1

Related Questions