This example shows you how to make a line plot using the psyplot.project.ProjectPlotter.lineplot
method.
import psyplot.project as psy
import numpy as np
%config InlineBackend.close_figures = False
axes = iter(psy.multiple_subplots(2, 2, n=3))
for var in ['t2m', 'u', 'v']:
psy.plot.lineplot(
'demo.nc', # netCDF file storing the data
name=var, # one plot for each variable
t=range(5), # one line for each time step
z=0, x=0, # choose latitude and longitude as dimensions
ylabel="{desc}", # use the longname and units on the y-axis
ax=next(axes),
color='coolwarm',
)
Okay, now we want to show the legend. As we show one line per timestep (and actually it's one line per month), we might want to label the lines accordingly. The legendlabels
formatoption can be automatically used for this, and we can use pythons built-in datetime formatting to format the time.
sp = psy.gcp(True)
sp.update(
legendlabels="%B", # use the Month as label for the legend
)
sp.show()
The legend is the same for all plots, so let's disable it for the last two, and show it for the first one above the plots. We can use the legend
formatoption for this, that accepts a boolean (to enable or disable the legend), or a dictionary with keyword arguments for matplotlibs legend function.
sp[1:].update(legend=False) # disable the legend for the second and third plot
sp[0].psy.update(
legend=dict(
title="Month",
loc="upper center",
bbox_to_anchor=(1.1, 1.3),
ncol=5,
fontsize="xx-large",
title_fontsize="xx-large",
)
)
sp.show()
sp.close()
The lineplot
plotmethod also supports the visualization of uncertainty intervals. For this example, let's assume the horizontal windspeed u
has an uncertainty of 3 meter per second.
We save this constant variable in our dataset as u_std
ds = psy.open_dataset("demo.nc")
ds["u_std"] = ds.u.copy(data=np.ones_like(ds.u) * 3)
ds
array([ 0. , 1.875, 3.75 , 5.625, 7.5 , 9.375, 11.25 , 13.125, 15. , 16.875, 18.75 , 20.625, 22.5 , 24.375, 26.25 , 28.125, 30. , 31.875, 33.75 , 35.625, 37.5 , 39.375, 41.25 , 43.125, 45. , 46.875, 48.75 , 50.625, 52.5 , 54.375, 56.25 , 58.125, 60. , 61.875, 63.75 , 65.625, 67.5 , 69.375, 71.25 , 73.125, 75. , 76.875, 78.75 , 80.625, 82.5 , 84.375, 86.25 , 88.125, 90. , 91.875, 93.75 , 95.625, 97.5 , 99.375, 101.25 , 103.125, 105. , 106.875, 108.75 , 110.625, 112.5 , 114.375, 116.25 , 118.125, 120. , 121.875, 123.75 , 125.625, 127.5 , 129.375, 131.25 , 133.125, 135. , 136.875, 138.75 , 140.625, 142.5 , 144.375, 146.25 , 148.125, 150. , 151.875, 153.75 , 155.625, 157.5 , 159.375, 161.25 , 163.125, 165. , 166.875, 168.75 , 170.625, 172.5 , 174.375, 176.25 , 178.125, 180. , 181.875, 183.75 , 185.625, 187.5 , 189.375, 191.25 , 193.125, 195. , 196.875, 198.75 , 200.625, 202.5 , 204.375, 206.25 , 208.125, 210. , 211.875, 213.75 , 215.625, 217.5 , 219.375, 221.25 , 223.125, 225. , 226.875, 228.75 , 230.625, 232.5 , 234.375, 236.25 , 238.125, 240. , 241.875, 243.75 , 245.625, 247.5 , 249.375, 251.25 , 253.125, 255. , 256.875, 258.75 , 260.625, 262.5 , 264.375, 266.25 , 268.125, 270. , 271.875, 273.75 , 275.625, 277.5 , 279.375, 281.25 , 283.125, 285. , 286.875, 288.75 , 290.625, 292.5 , 294.375, 296.25 , 298.125, 300. , 301.875, 303.75 , 305.625, 307.5 , 309.375, 311.25 , 313.125, 315. , 316.875, 318.75 , 320.625, 322.5 , 324.375, 326.25 , 328.125, 330. , 331.875, 333.75 , 335.625, 337.5 , 339.375, 341.25 , 343.125, 345. , 346.875, 348.75 , 350.625, 352.5 , 354.375, 356.25 , 358.125])
array([ 88.572169, 86.722531, 84.86197 , 82.998942, 81.134977, 79.270559, 77.405888, 75.541061, 73.676132, 71.811132, 69.946081, 68.080991, 66.215872, 64.35073 , 62.485571, 60.620396, 58.755209, 56.890013, 55.024808, 53.159595, 51.294377, 49.429154, 47.563926, 45.698694, 43.833459, 41.96822 , 40.102979, 38.237736, 36.372491, 34.507243, 32.641994, 30.776744, 28.911492, 27.046239, 25.180986, 23.315731, 21.450475, 19.585219, 17.719962, 15.854704, 13.989446, 12.124187, 10.258928, 8.393669, 6.528409, 4.66315 , 2.79789 , 0.93263 , -0.93263 , -2.79789 , -4.66315 , -6.528409, -8.393669, -10.258928, -12.124187, -13.989446, -15.854704, -17.719962, -19.585219, -21.450475, -23.315731, -25.180986, -27.046239, -28.911492, -30.776744, -32.641994, -34.507243, -36.372491, -38.237736, -40.102979, -41.96822 , -43.833459, -45.698694, -47.563926, -49.429154, -51.294377, -53.159595, -55.024808, -56.890013, -58.755209, -60.620396, -62.485571, -64.35073 , -66.215872, -68.080991, -69.946081, -71.811132, -73.676132, -75.541061, -77.405888, -79.270559, -81.134977, -82.998942, -84.86197 , -86.722531, -88.572169])
array([100000., 85000., 50000., 20000.])
array(['1979-01-31T18:00:00.000000000', '1979-02-28T18:00:00.000000000', '1979-03-31T18:00:00.000000000', '1979-04-30T18:00:00.000000000', '1979-05-31T18:00:00.000000000'], dtype='datetime64[ns]')
[368640 values with dtype=float32]
array([[[[-5.548854, ..., -5.618678], ..., [-3.48733 , ..., -3.599635]], ..., [[ 0.567179, ..., 0.379679], ..., [ 0.028116, ..., 0.063272]]], ..., [[[ 3.058149, ..., 3.079634], ..., [-7.162554, ..., -7.268511]], ..., [[ 1.471132, ..., 1.528749], ..., [-3.798399, ..., -3.843321]]]], dtype=float32)
[368640 values with dtype=float32]
array([[[[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]]], [[[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]]], [[[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]]], [[[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]]], [[[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]], [[3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], ..., [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.], [3., 3., 3., ..., 3., 3., 3.]]]], dtype=float32)
To visualize this uncertainty, we need to pass it as name=[[["u", "u_std"]]]
to the lineplot method.
sp = ds.psy.plot.lineplot(
name=[[["u", "u_std"]]],
t=range(2), # one line for each time step
z=0, x=0, # choose latitude and longitude as dimensions
ylabel="{desc}", # use the longname and units on the y-axis
xlabel="{desc}", # use the longname and units on the x-axis
color="coolwarm",
legendlabels="%B", # use the Month as label for the legend ()
legend={"loc": "upper center", "bbox_to_anchor": (0.5, 1.2), "ncol": 2, "title": "Month"},
)
This structure parameters for name
is important here, because it tells the lineplot how to structure the data. Using this syntax, our project sp
now holds a list two 2D arrays, one for each line.
sp
psyplot.project.Project([arr3: psyplot.data.InteractiveList([ arr0: 2-dim DataArray of u, u_std, with (variable, lat)=(2, 96), time=1979-01-31T18:00:00, lev=100000.0, lon=0.0, arr1: 2-dim DataArray of u, u_std, with (variable, lat)=(2, 96), time=1979-02-28T18:00:00, lev=100000.0, lon=0.0])])
Using name=[[["u", "u_std"]]]
is effectively the same as using ds[["u", "u_std"]].to_array()
.
sp[0][0]
array([[-5.548854 , -4.1748304 , -2.2866468 , -1.2041273 , -1.835475 , -3.1245375 , -3.7724867 , -3.7163343 , -3.3364515 , -2.7407484 , -2.1152601 , -1.4316664 , -0.8032484 , 0.01755238, 0.7587633 , 1.0795641 , 1.9267321 , 3.5702868 , 4.11765 , 3.4946032 , 3.5873766 , 3.5903063 , 2.2924547 , 1.7192125 , 2.393529 , 2.643529 , 2.5780993 , 3.2187243 , 3.2275133 , 1.6640368 , 0.43259144, -0.21877575, -1.1460218 , -1.469264 , -0.8994398 , -0.9209242 , -1.9463148 , -3.1084242 , -3.9131117 , -4.0761976 , -3.634303 , -3.0571547 , -2.1577406 , -0.6006117 , 0.8031969 , 1.2529039 , 0.59860706, -0.02248669, -0.33303356, -0.25539684, 0.05417347, 0.18845081, -0.22219372, -1.2588148 , -2.6782484 , -3.9194593 , -5.0395765 , -6.418483 , -7.7617445 , -8.567897 , -8.929713 , -8.928248 , -8.342311 , -6.918971 , -4.7202406 , -2.0053968 , 1.258275 , 4.8202868 , 7.832494 , 10.224584 , 11.624974 , 11.937963 , 11.861791 , 11.053197 , 9.686986 , 7.794896 , 6.019994 , 4.9150133 , 2.8803453 , 0.35202503, -0.441432 , -0.0273695 , 0.28366566, 0.48972034, -0.35500622, -2.929225 , -4.9565687 , -4.361354 , -1.8227797 , 0.34177113, 0.1547594 , -1.6777601 , -2.3779554 , -1.9463148 , -2.6640882 , -3.4873304 ], [ 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. ]], dtype=float32)
array('1979-01-31T18:00:00.000000000', dtype='datetime64[ns]')
array([ 88.572169, 86.722531, 84.86197 , 82.998942, 81.134977, 79.270559, 77.405888, 75.541061, 73.676132, 71.811132, 69.946081, 68.080991, 66.215872, 64.35073 , 62.485571, 60.620396, 58.755209, 56.890013, 55.024808, 53.159595, 51.294377, 49.429154, 47.563926, 45.698694, 43.833459, 41.96822 , 40.102979, 38.237736, 36.372491, 34.507243, 32.641994, 30.776744, 28.911492, 27.046239, 25.180986, 23.315731, 21.450475, 19.585219, 17.719962, 15.854704, 13.989446, 12.124187, 10.258928, 8.393669, 6.528409, 4.66315 , 2.79789 , 0.93263 , -0.93263 , -2.79789 , -4.66315 , -6.528409, -8.393669, -10.258928, -12.124187, -13.989446, -15.854704, -17.719962, -19.585219, -21.450475, -23.315731, -25.180986, -27.046239, -28.911492, -30.776744, -32.641994, -34.507243, -36.372491, -38.237736, -40.102979, -41.96822 , -43.833459, -45.698694, -47.563926, -49.429154, -51.294377, -53.159595, -55.024808, -56.890013, -58.755209, -60.620396, -62.485571, -64.35073 , -66.215872, -68.080991, -69.946081, -71.811132, -73.676132, -75.541061, -77.405888, -79.270559, -81.134977, -82.998942, -84.86197 , -86.722531, -88.572169])
array(100000.)
array(0.)
array(['u', 'u_std'], dtype='<U5')
ds[["u", "u_std"]].isel(time=0, lon=0, lev=0).to_array()
array([[-5.548854 , -4.1748304 , -2.2866468 , -1.2041273 , -1.835475 , -3.1245375 , -3.7724867 , -3.7163343 , -3.3364515 , -2.7407484 , -2.1152601 , -1.4316664 , -0.8032484 , 0.01755238, 0.7587633 , 1.0795641 , 1.9267321 , 3.5702868 , 4.11765 , 3.4946032 , 3.5873766 , 3.5903063 , 2.2924547 , 1.7192125 , 2.393529 , 2.643529 , 2.5780993 , 3.2187243 , 3.2275133 , 1.6640368 , 0.43259144, -0.21877575, -1.1460218 , -1.469264 , -0.8994398 , -0.9209242 , -1.9463148 , -3.1084242 , -3.9131117 , -4.0761976 , -3.634303 , -3.0571547 , -2.1577406 , -0.6006117 , 0.8031969 , 1.2529039 , 0.59860706, -0.02248669, -0.33303356, -0.25539684, 0.05417347, 0.18845081, -0.22219372, -1.2588148 , -2.6782484 , -3.9194593 , -5.0395765 , -6.418483 , -7.7617445 , -8.567897 , -8.929713 , -8.928248 , -8.342311 , -6.918971 , -4.7202406 , -2.0053968 , 1.258275 , 4.8202868 , 7.832494 , 10.224584 , 11.624974 , 11.937963 , 11.861791 , 11.053197 , 9.686986 , 7.794896 , 6.019994 , 4.9150133 , 2.8803453 , 0.35202503, -0.441432 , -0.0273695 , 0.28366566, 0.48972034, -0.35500622, -2.929225 , -4.9565687 , -4.361354 , -1.8227797 , 0.34177113, 0.1547594 , -1.6777601 , -2.3779554 , -1.9463148 , -2.6640882 , -3.4873304 ], [ 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. , 3. ]], dtype=float32)
array('1979-01-31T18:00:00.000000000', dtype='datetime64[ns]')
array([ 88.572169, 86.722531, 84.86197 , 82.998942, 81.134977, 79.270559, 77.405888, 75.541061, 73.676132, 71.811132, 69.946081, 68.080991, 66.215872, 64.35073 , 62.485571, 60.620396, 58.755209, 56.890013, 55.024808, 53.159595, 51.294377, 49.429154, 47.563926, 45.698694, 43.833459, 41.96822 , 40.102979, 38.237736, 36.372491, 34.507243, 32.641994, 30.776744, 28.911492, 27.046239, 25.180986, 23.315731, 21.450475, 19.585219, 17.719962, 15.854704, 13.989446, 12.124187, 10.258928, 8.393669, 6.528409, 4.66315 , 2.79789 , 0.93263 , -0.93263 , -2.79789 , -4.66315 , -6.528409, -8.393669, -10.258928, -12.124187, -13.989446, -15.854704, -17.719962, -19.585219, -21.450475, -23.315731, -25.180986, -27.046239, -28.911492, -30.776744, -32.641994, -34.507243, -36.372491, -38.237736, -40.102979, -41.96822 , -43.833459, -45.698694, -47.563926, -49.429154, -51.294377, -53.159595, -55.024808, -56.890013, -58.755209, -60.620396, -62.485571, -64.35073 , -66.215872, -68.080991, -69.946081, -71.811132, -73.676132, -75.541061, -77.405888, -79.270559, -81.134977, -82.998942, -84.86197 , -86.722531, -88.572169])
array(100000.)
array(0.)
array(['u', 'u_std'], dtype='<U5')
psy.close('all')