CodeBreak 27/10/2021

Revision as of 19:19, 3 November 2021 by C.carouge (talk | contribs) (Created page with " === <span style="font-size:13.999999999999998pt; font-family:Arial; color:#434343; background-color:transparent; font-weight:400; font-style:normal; font-variant:normal...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Panel plot along the time axis

How to plot all time slices of a 3D dataset (time, latitude, longitude) in a single figure? We settled on this:

from itertools import chain
fig, axes = plt.subplot(7, 4, figsize=(20, 40), 
                        subplot_kw={'projection': ccrs.PlateCarree()})
for</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">t, ax </span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#0000ff;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">in</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">enumerate(chain(*axes)):</span>
<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">    field = mydata.field.isel(time=t)</span>
<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">    field.plot(ax=ax, x=</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#a31515;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">'latitude'</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">, y=</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#a31515;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">'longitude'</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">, </span>
<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">              add_colorbar=</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#0000ff;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">False</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">, transform=ccrs.PlateCarree())</span>
<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">    ax.coastlines()</span>
<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">    ax.set_title(mydata.time[t].item())</span>

<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#008000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap"># add latitude ticks for first column</span>
<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#0000ff;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">for</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">j </span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#0000ff;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">in</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">range(7):</span>
<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">    axes[j][0].set_yticks(np.linspace(-90, 90, 5, endpoint=</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#0000ff;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">True</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">), </span>
<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">                          transform=ccrs.PlateCarree())</span>

<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#008000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap"># add longitude ticks for final row</span>
<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#0000ff;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">for</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">j </span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#0000ff;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">in</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">range(4):</span>
<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">    axes[-1][j].set_xticks(np.linspace(-180, 180, 5, endpoint=</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#0000ff;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">True</span><span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">), </span>
<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">                          transform=ccrs.PlateCarree())</span>
<span style="font-size:11pt;  font-family:Consolas,sans-serif;  color:#000000;  background-color:#ffffff;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">plt.show()</span>