Difference between revisions of "CodeBreak 27/10/2021"

(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...")
 
Line 1: Line 1:
 +
  
 
=== <span style="font-size:13.999999999999998pt;  font-family:Arial;  color:#434343;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">Panel plot along the time axis</span> ===
 
=== <span style="font-size:13.999999999999998pt;  font-family:Arial;  color:#434343;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">Panel plot along the time axis</span> ===
  
<span style="font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">How to plot all time slices of a 3D dataset (time, latitude, longitude) in a single figure? We settled on this:</span>
+
<span style="font-size:11pt;  font-family:Arial;  color:#000000;  background-color:transparent;  font-weight:400;  font-style:normal;  font-variant:normal;  text-decoration:none;  vertical-align:baseline;  white-space:pre;  white-space:pre-wrap">How to plot all time slices of a 3D dataset (time, latitude, longitude) in a single figure? We settled on this:</span> from itertools import chain
<pre>
 
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></pre>
 
<div dir="ltr" style="margin-left:0pt" align="left">
 
{| style="border:none;  border-collapse:collapse"
 
|- style="height:0pt"
 
| style="vertical-align:top;  background-color:#ffffff;  padding:5pt 5pt 5pt 5pt;  overflow:hidden;  overflow-wrap:break-word" |
 
&nbsp;
 
  
|}
+
fig, axes = plt.subplot(7, 4, figsize=(20, 40),
</div>
+
                        subplot_kw={'projection': ccrs.PlateCarree()})
 +
 +
for t, ax in enumerate(chain(*axes)):
 +
    field = mydata.field.isel(time=t)
 +
    field.plot(ax=ax, x='latitude', y='longitude',
 +
                add_colorbar=False, transform=ccrs.PlateCarree())
 +
    ax.coastlines()
 +
    ax.set_title(mydata.time[t].item())
 +
 +
# add latitude ticks for first column
 +
for j in range(7):
 +
    axes[j][0].set_yticks(np.linspace(-90, 90, 5, endpoint=True),
 +
                          transform=ccrs.PlateCarree())
 +
 +
# add longitude ticks for final row
 +
for j in range(4):
 +
    axes[-1][j].set_xticks(np.linspace(-180, 180, 5, endpoint=True),
 +
                            transform=ccrs.PlateCarree())
 +
plt.show()

Revision as of 19:37, 3 November 2021


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 t, ax in enumerate(chain(*axes)):
    field = mydata.field.isel(time=t)
    field.plot(ax=ax, x='latitude', y='longitude', 
               add_colorbar=False, transform=ccrs.PlateCarree())
    ax.coastlines()
    ax.set_title(mydata.time[t].item())

# add latitude ticks for first column
for j in range(7):
    axes[j][0].set_yticks(np.linspace(-90, 90, 5, endpoint=True), 
                          transform=ccrs.PlateCarree())

# add longitude ticks for final row
for j in range(4):
    axes[-1][j].set_xticks(np.linspace(-180, 180, 5, endpoint=True), 
                           transform=ccrs.PlateCarree())
plt.show()