From ba38757c11322a9b85f2922fccbab8ed61d6f417 Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Tue, 26 Jan 2021 07:45:24 -0500 Subject: [PATCH 01/20] Create box3d.py Suggestion from https://github.com/matplotlib/matplotlib/issues/19310 --- examples/mplot3d/box3d.py | 110 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 examples/mplot3d/box3d.py diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py new file mode 100644 index 000000000000..ca9bf9634474 --- /dev/null +++ b/examples/mplot3d/box3d.py @@ -0,0 +1,110 @@ +''' +================================= +3D surface plot using Matplotlib +================================= +by: Iury T. Simoes-Sousa (iuryt) + +The strategy is to select the data from each surface and plot contours separately. +To use this feature you need to have gridded coordinates. +The contour plot from Matplotlib has zdir argument that defines the normal coordinate to the plotted surface. +The offset argument defines the offset applied to the contourf surface. +''' + + + +import matplotlib.pyplot as plt +import numpy as np +import xarray as xr + +#Define dimensions +Nx,Ny,Nz = 100,300,500 +X = xr.DataArray(np.arange(Nx),dims=['X']) +Y = xr.DataArray(np.arange(Ny),dims=['Y']) +Z = xr.DataArray(-np.arange(Nz),dims=['Z']) + +#Create fake data +#This code works for da => ZYX. +#For da => XYZ or YXZ or ZYX you may have to change the plotting part. +# or transpose the data before plotting +da = xr.DataArray( + ((X+100)**2 + (Y-20)**2 + 2*Z)/1000+1, + coords={'X':X,'Y':Y,'Z':Z}, + dims=['X','Y','Z'] +).T #.T invert from XYZ to ZYX + + +vmin = da.min().values +vmax = da.max().values +#Key arguments for contour plots +kw = { + 'vmin':vmin, + 'vmax':vmax, + 'levels':np.linspace(vmin,vmax,10), + 'cmap':'viridis', +} + +#Create a figure with 3D ax +fig = plt.figure(figsize=(7,4)) +ax = fig.add_subplot(111,projection='3d') + +#Upper surface-- +#Select the surface at Z=0 +di = da.sel(Z=0,method='nearest') +#Create the grid for plotting (required for 3D plots) +dims = np.meshgrid(di[di.dims[1]].values,di[di.dims[0]].values) +#Plot surface +#zdir sets the normal axis and +#offset is the surface offset at this normal axis +C = ax.contourf(dims[0],dims[1],di.values,zdir='z',offset=0,**kw) +# -- + + +#South surface-- +#Select the face at Y=0 +di = da.sel(Y=0) +#Create the grid for plotting (required for 3D plots) +dims = np.meshgrid(di[di.dims[1]].values,di[di.dims[0]].values) +#Plot surface +#zdir sets the normal axis and +#offset is the surface offset at this normal axis +C = ax.contourf(dims[0],di.values,dims[1],zdir='y',offset=di.Y.values,**kw) + +#East surface-- +#Select the face at X=X.max() +di = da.sel(X=da.X.max(),method='nearest') +#Create the grid for plotting (required for 3D plots) +dims = np.meshgrid(di[di.dims[1]].values,di[di.dims[0]].values) +#Plot surface +#zdir sets the normal axis and +#offset is the surface offset at this normal axis +C = ax.contourf(di.values,dims[0],dims[1],zdir='x',offset=di.X.values,**kw) + +#Set limits of the plot from coord limits +ax.set( + xlim=[da.X.min(),da.X.max()], + ylim=[da.Y.min(),da.Y.max()], + zlim=[da.Z.min(),da.Z.max()], +) + +color = '0.4' #color of the line of the corners +#Get xlim,ylim and zlim +xlim,ylim,zlim = list(map(np.array,[ax.get_xlim(),ax.get_ylim(),ax.get_zlim()])) +#Plot corners +ax.plot(xlim*0+xlim[1],ylim,zlim*0,color,linewidth=1,zorder=1e4) +ax.plot(xlim,ylim*0+ylim[0],zlim*0,color,linewidth=1,zorder=1e4) +ax.plot(xlim*0+xlim[1],ylim*0+ylim[0],zlim,color,linewidth=1,zorder=1e4) + +#Set labels and zticks +ax.set( + xlabel='\n X [km]', + ylabel='\n Y [km]', + zlabel='\n Z [m]', + zticks=[0,-150,-300,-450], +) + +#Set distance and angle view +ax.view_init(40, -30) +ax.dist = 11 + +#Colorbar +fig.colorbar(C,ax=ax,fraction=0.02,pad=0.1,label='Name [units]') From 8852be66fcde0b4af1a3972c7993338b74e8fbdd Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Tue, 26 Jan 2021 09:26:25 -0500 Subject: [PATCH 02/20] Update box3d.py Fix missing whitespaces and replace single by double quotes --- examples/mplot3d/box3d.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index ca9bf9634474..af0a98d5da02 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -1,26 +1,28 @@ -''' +""" ================================= 3D surface plot using Matplotlib ================================= by: Iury T. Simoes-Sousa (iuryt) -The strategy is to select the data from each surface and plot contours separately. +The strategy is to select the data from each surface and plot +contours separately. To use this feature you need to have gridded coordinates. -The contour plot from Matplotlib has zdir argument that defines the normal coordinate to the plotted surface. -The offset argument defines the offset applied to the contourf surface. -''' +The contour plot from Matplotlib has zdir argument that defines +the normal coordinate to the plotted surface. +The offset argument defines the offset applied to the contourf surface. +""" import matplotlib.pyplot as plt import numpy as np import xarray as xr #Define dimensions -Nx,Ny,Nz = 100,300,500 -X = xr.DataArray(np.arange(Nx),dims=['X']) -Y = xr.DataArray(np.arange(Ny),dims=['Y']) -Z = xr.DataArray(-np.arange(Nz),dims=['Z']) +Nx, Ny, Nz = 100, 300, 500 +X = xr.DataArray(np.arange(Nx), dims=['X']) +Y = xr.DataArray(np.arange(Ny), dims=['Y']) +Z = xr.DataArray(-np.arange(Nz), dims=['Z']) #Create fake data #This code works for da => ZYX. From cb35724e35566b0e6f895469ad6a3751b0b308d9 Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Tue, 26 Jan 2021 09:41:00 -0500 Subject: [PATCH 03/20] Update box3d.py Add missing whitespaces --- examples/mplot3d/box3d.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index af0a98d5da02..3f708ddbeb81 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -4,11 +4,11 @@ ================================= by: Iury T. Simoes-Sousa (iuryt) -The strategy is to select the data from each surface and plot +The strategy is to select the data from each surface and plot contours separately. To use this feature you need to have gridded coordinates. -The contour plot from Matplotlib has zdir argument that defines +The contour plot from Matplotlib has zdir argument that defines the normal coordinate to the plotted surface. The offset argument defines the offset applied to the contourf surface. @@ -30,8 +30,8 @@ # or transpose the data before plotting da = xr.DataArray( ((X+100)**2 + (Y-20)**2 + 2*Z)/1000+1, - coords={'X':X,'Y':Y,'Z':Z}, - dims=['X','Y','Z'] + coords={'X':X, 'Y':Y, 'Z':Z}, + dims=['X', 'Y', 'Z'] ).T #.T invert from XYZ to ZYX @@ -47,17 +47,17 @@ #Create a figure with 3D ax fig = plt.figure(figsize=(7,4)) -ax = fig.add_subplot(111,projection='3d') +ax = fig.add_subplot(111, projection='3d') #Upper surface-- #Select the surface at Z=0 -di = da.sel(Z=0,method='nearest') +di = da.sel(Z=0, method='nearest') #Create the grid for plotting (required for 3D plots) -dims = np.meshgrid(di[di.dims[1]].values,di[di.dims[0]].values) +dims = np.meshgrid(di[di.dims[1]].values, di[di.dims[0]].values) #Plot surface #zdir sets the normal axis and #offset is the surface offset at this normal axis -C = ax.contourf(dims[0],dims[1],di.values,zdir='z',offset=0,**kw) +C = ax.contourf(dims[0], dims[1], di.values, zdir='z', offset=0, **kw) # -- @@ -65,27 +65,27 @@ #Select the face at Y=0 di = da.sel(Y=0) #Create the grid for plotting (required for 3D plots) -dims = np.meshgrid(di[di.dims[1]].values,di[di.dims[0]].values) +dims = np.meshgrid(di[di.dims[1]].values, di[di.dims[0]].values) #Plot surface #zdir sets the normal axis and #offset is the surface offset at this normal axis -C = ax.contourf(dims[0],di.values,dims[1],zdir='y',offset=di.Y.values,**kw) +C = ax.contourf(dims[0], di.values, dims[1], zdir='y', offset=di.Y.values, **kw) #East surface-- #Select the face at X=X.max() -di = da.sel(X=da.X.max(),method='nearest') +di = da.sel(X=da.X.max(), method='nearest') #Create the grid for plotting (required for 3D plots) -dims = np.meshgrid(di[di.dims[1]].values,di[di.dims[0]].values) +dims = np.meshgrid(di[di.dims[1]].values, di[di.dims[0]].values) #Plot surface #zdir sets the normal axis and #offset is the surface offset at this normal axis -C = ax.contourf(di.values,dims[0],dims[1],zdir='x',offset=di.X.values,**kw) +C = ax.contourf(di.values, dims[0], dims[1], zdir='x', offset=di.X.values, **kw) #Set limits of the plot from coord limits ax.set( - xlim=[da.X.min(),da.X.max()], - ylim=[da.Y.min(),da.Y.max()], - zlim=[da.Z.min(),da.Z.max()], + xlim=[da.X.min(), da.X.max()], + ylim=[da.Y.min(), da.Y.max()], + zlim=[da.Z.min(), da.Z.max()], ) color = '0.4' #color of the line of the corners @@ -109,4 +109,4 @@ ax.dist = 11 #Colorbar -fig.colorbar(C,ax=ax,fraction=0.02,pad=0.1,label='Name [units]') +fig.colorbar(C, ax=ax, fraction=0.02, pad=0.1, label='Name [units]') From 76024ea94fea6871191267207389198ab5082699 Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Tue, 26 Jan 2021 09:45:44 -0500 Subject: [PATCH 04/20] Update box3d.py Incorporate pflakes suggestions --- examples/mplot3d/box3d.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index 3f708ddbeb81..e3f9a24334f9 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -24,25 +24,25 @@ Y = xr.DataArray(np.arange(Ny), dims=['Y']) Z = xr.DataArray(-np.arange(Nz), dims=['Z']) -#Create fake data -#This code works for da => ZYX. +#Create fake data +#This code works for da => ZYX. #For da => XYZ or YXZ or ZYX you may have to change the plotting part. # or transpose the data before plotting da = xr.DataArray( ((X+100)**2 + (Y-20)**2 + 2*Z)/1000+1, - coords={'X':X, 'Y':Y, 'Z':Z}, + coords={'X': X, 'Y': Y, 'Z': Z}, dims=['X', 'Y', 'Z'] -).T #.T invert from XYZ to ZYX +).T #.T invert from XYZ to ZYX vmin = da.min().values vmax = da.max().values #Key arguments for contour plots kw = { - 'vmin':vmin, - 'vmax':vmax, - 'levels':np.linspace(vmin,vmax,10), - 'cmap':'viridis', + 'vmin': vmin, + 'vmax': vmax, + 'levels': np.linspace(vmin,vmax,10), + 'cmap': 'viridis', } #Create a figure with 3D ax @@ -56,9 +56,9 @@ dims = np.meshgrid(di[di.dims[1]].values, di[di.dims[0]].values) #Plot surface #zdir sets the normal axis and -#offset is the surface offset at this normal axis +#offset is the surface offset at this normal axis C = ax.contourf(dims[0], dims[1], di.values, zdir='z', offset=0, **kw) -# -- +# -- #South surface-- @@ -68,7 +68,7 @@ dims = np.meshgrid(di[di.dims[1]].values, di[di.dims[0]].values) #Plot surface #zdir sets the normal axis and -#offset is the surface offset at this normal axis +#offset is the surface offset at this normal axis C = ax.contourf(dims[0], di.values, dims[1], zdir='y', offset=di.Y.values, **kw) #East surface-- @@ -78,7 +78,7 @@ dims = np.meshgrid(di[di.dims[1]].values, di[di.dims[0]].values) #Plot surface #zdir sets the normal axis and -#offset is the surface offset at this normal axis +#offset is the surface offset at this normal axis C = ax.contourf(di.values, dims[0], dims[1], zdir='x', offset=di.X.values, **kw) #Set limits of the plot from coord limits From 0eb24d9fa1d6f3136fee16c7e7f01e16a129ec7e Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Tue, 26 Jan 2021 09:56:06 -0500 Subject: [PATCH 05/20] Update box3d.py Incorporate pflakes suggestions --- examples/mplot3d/box3d.py | 88 +++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 41 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index e3f9a24334f9..eb3073da828d 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -18,95 +18,101 @@ import numpy as np import xarray as xr -#Define dimensions +# Define dimensions Nx, Ny, Nz = 100, 300, 500 X = xr.DataArray(np.arange(Nx), dims=['X']) Y = xr.DataArray(np.arange(Ny), dims=['Y']) Z = xr.DataArray(-np.arange(Nz), dims=['Z']) -#Create fake data -#This code works for da => ZYX. -#For da => XYZ or YXZ or ZYX you may have to change the plotting part. +# Create fake data +# This code works for da => ZYX. +# For da => XYZ or YXZ or ZYX you may have to change the plotting part. # or transpose the data before plotting da = xr.DataArray( ((X+100)**2 + (Y-20)**2 + 2*Z)/1000+1, coords={'X': X, 'Y': Y, 'Z': Z}, dims=['X', 'Y', 'Z'] -).T #.T invert from XYZ to ZYX +).T # .T invert from XYZ to ZYX vmin = da.min().values vmax = da.max().values -#Key arguments for contour plots +# Key arguments for contour plots kw = { 'vmin': vmin, 'vmax': vmax, - 'levels': np.linspace(vmin,vmax,10), + 'levels': np.linspace(vmin, vmax, 10), 'cmap': 'viridis', } -#Create a figure with 3D ax -fig = plt.figure(figsize=(7,4)) +# Create a figure with 3D ax +fig = plt.figure(figsize=(7, 4)) ax = fig.add_subplot(111, projection='3d') -#Upper surface-- -#Select the surface at Z=0 +# Upper surface-- +# Select the surface at Z=0 di = da.sel(Z=0, method='nearest') -#Create the grid for plotting (required for 3D plots) +# Create the grid for plotting (required for 3D plots) dims = np.meshgrid(di[di.dims[1]].values, di[di.dims[0]].values) -#Plot surface -#zdir sets the normal axis and -#offset is the surface offset at this normal axis +# Plot surface +# zdir sets the normal axis and +# offset is the surface offset at this normal axis C = ax.contourf(dims[0], dims[1], di.values, zdir='z', offset=0, **kw) # -- -#South surface-- -#Select the face at Y=0 +# South surface-- +# Select the face at Y=0 di = da.sel(Y=0) -#Create the grid for plotting (required for 3D plots) +# Create the grid for plotting (required for 3D plots) dims = np.meshgrid(di[di.dims[1]].values, di[di.dims[0]].values) -#Plot surface -#zdir sets the normal axis and -#offset is the surface offset at this normal axis -C = ax.contourf(dims[0], di.values, dims[1], zdir='y', offset=di.Y.values, **kw) +# Plot surface +# zdir sets the normal axis and +# offset is the surface offset at this normal axis +C = ax.contourf( + dims[0], di.values, dims[1], zdir='y', + offset=di.Y.values, **kw, +) -#East surface-- -#Select the face at X=X.max() +# East surface-- +# Select the face at X=X.max() di = da.sel(X=da.X.max(), method='nearest') -#Create the grid for plotting (required for 3D plots) +# Create the grid for plotting (required for 3D plots) dims = np.meshgrid(di[di.dims[1]].values, di[di.dims[0]].values) -#Plot surface -#zdir sets the normal axis and -#offset is the surface offset at this normal axis -C = ax.contourf(di.values, dims[0], dims[1], zdir='x', offset=di.X.values, **kw) +# Plot surface +# zdir sets the normal axis and +# offset is the surface offset at this normal axis +C = ax.contourf( + di.values, dims[0], dims[1], + zdir='x', offset=di.X.values, **kw +) -#Set limits of the plot from coord limits +# Set limits of the plot from coord limits ax.set( xlim=[da.X.min(), da.X.max()], ylim=[da.Y.min(), da.Y.max()], zlim=[da.Z.min(), da.Z.max()], ) -color = '0.4' #color of the line of the corners -#Get xlim,ylim and zlim -xlim,ylim,zlim = list(map(np.array,[ax.get_xlim(),ax.get_ylim(),ax.get_zlim()])) -#Plot corners -ax.plot(xlim*0+xlim[1],ylim,zlim*0,color,linewidth=1,zorder=1e4) -ax.plot(xlim,ylim*0+ylim[0],zlim*0,color,linewidth=1,zorder=1e4) -ax.plot(xlim*0+xlim[1],ylim*0+ylim[0],zlim,color,linewidth=1,zorder=1e4) +color = '0.4' # color of the line of the corners +# Get xlim,ylim and zlim +xlim, ylim, zlim = list(map(np.array, [ax.get_xlim(), ax.get_ylim(), ax.get_zlim()])) +# Plot corners +ax.plot(xlim*0+xlim[1], ylim,zlim*0, color, linewidth=1, zorder=1e4) +ax.plot(xlim, ylim*0+ylim[0], zlim*0, color, linewidth=1, zorder=1e4) +ax.plot(xlim*0+xlim[1], ylim*0+ylim[0], zlim, color, linewidth=1, zorder=1e4) -#Set labels and zticks +# Set labels and zticks ax.set( xlabel='\n X [km]', ylabel='\n Y [km]', zlabel='\n Z [m]', - zticks=[0,-150,-300,-450], + zticks=[0, -150, -300, -450], ) -#Set distance and angle view +# Set distance and angle view ax.view_init(40, -30) ax.dist = 11 -#Colorbar +# Colorbar fig.colorbar(C, ax=ax, fraction=0.02, pad=0.1, label='Name [units]') From 719e6987faf862be754c16892f3db4524ac8db9b Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Tue, 26 Jan 2021 10:02:36 -0500 Subject: [PATCH 06/20] Update box3d.py Incorporated PyLint suggestions --- examples/mplot3d/box3d.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index eb3073da828d..d676d5216701 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -96,11 +96,23 @@ color = '0.4' # color of the line of the corners # Get xlim,ylim and zlim -xlim, ylim, zlim = list(map(np.array, [ax.get_xlim(), ax.get_ylim(), ax.get_zlim()])) +xlim = np.array(ax.get_xlim()) +ylim = np.array(ax.get_ylim()) +zlim = np.array(ax.get_zlim()) + # Plot corners -ax.plot(xlim*0+xlim[1], ylim,zlim*0, color, linewidth=1, zorder=1e4) -ax.plot(xlim, ylim*0+ylim[0], zlim*0, color, linewidth=1, zorder=1e4) -ax.plot(xlim*0+xlim[1], ylim*0+ylim[0], zlim, color, linewidth=1, zorder=1e4) +ax.plot( + xlim*0+xlim[1], ylim, zlim*0, color, + linewidth=1, zorder=1e4, +) +ax.plot( + xlim, ylim*0+ylim[0], zlim*0, color, + linewidth=1, zorder=1e4, +) +ax.plot( + xlim*0+xlim[1], ylim*0+ylim[0], zlim, color, + linewidth=1, zorder=1e4, +) # Set labels and zticks ax.set( From 1b982e9eadcfe71a93a98c6d6f7cf57ef2b4df58 Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Wed, 27 Jan 2021 12:25:53 -0500 Subject: [PATCH 07/20] NumPy instead of Xarray --- examples/mplot3d/box3d.py | 65 ++++++++------------------------------- 1 file changed, 12 insertions(+), 53 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index d676d5216701..f62675224197 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -20,23 +20,13 @@ # Define dimensions Nx, Ny, Nz = 100, 300, 500 -X = xr.DataArray(np.arange(Nx), dims=['X']) -Y = xr.DataArray(np.arange(Ny), dims=['Y']) -Z = xr.DataArray(-np.arange(Nz), dims=['Z']) +X,Y,Z = np.meshgrid(np.arange(Nx), np.arange(Ny), -np.arange(Nz)) # Create fake data -# This code works for da => ZYX. -# For da => XYZ or YXZ or ZYX you may have to change the plotting part. -# or transpose the data before plotting -da = xr.DataArray( - ((X+100)**2 + (Y-20)**2 + 2*Z)/1000+1, - coords={'X': X, 'Y': Y, 'Z': Z}, - dims=['X', 'Y', 'Z'] -).T # .T invert from XYZ to ZYX - - -vmin = da.min().values -vmax = da.max().values +da = (((X+100)**2 + (Y-20)**2 + 2*Z)/1000+1) + +vmin = da.min() +vmax = da.max() # Key arguments for contour plots kw = { 'vmin': vmin, @@ -49,49 +39,18 @@ fig = plt.figure(figsize=(7, 4)) ax = fig.add_subplot(111, projection='3d') -# Upper surface-- -# Select the surface at Z=0 -di = da.sel(Z=0, method='nearest') -# Create the grid for plotting (required for 3D plots) -dims = np.meshgrid(di[di.dims[1]].values, di[di.dims[0]].values) -# Plot surface -# zdir sets the normal axis and -# offset is the surface offset at this normal axis -C = ax.contourf(dims[0], dims[1], di.values, zdir='z', offset=0, **kw) +# Plot contour surfaces +C = ax.contourf(X[:,:,0], Y[:,:,0], da[:,:,0], zdir='z', offset=0, **kw) +C = ax.contourf(X[0,:,:], da[0,:,:], Z[0,:,:], zdir='y', offset=0, **kw) +C = ax.contourf(da[:,-1,:], Y[:,-1,:], Z[:,-1,:], zdir='x', offset=X.max(), **kw) # -- -# South surface-- -# Select the face at Y=0 -di = da.sel(Y=0) -# Create the grid for plotting (required for 3D plots) -dims = np.meshgrid(di[di.dims[1]].values, di[di.dims[0]].values) -# Plot surface -# zdir sets the normal axis and -# offset is the surface offset at this normal axis -C = ax.contourf( - dims[0], di.values, dims[1], zdir='y', - offset=di.Y.values, **kw, -) - -# East surface-- -# Select the face at X=X.max() -di = da.sel(X=da.X.max(), method='nearest') -# Create the grid for plotting (required for 3D plots) -dims = np.meshgrid(di[di.dims[1]].values, di[di.dims[0]].values) -# Plot surface -# zdir sets the normal axis and -# offset is the surface offset at this normal axis -C = ax.contourf( - di.values, dims[0], dims[1], - zdir='x', offset=di.X.values, **kw -) - # Set limits of the plot from coord limits ax.set( - xlim=[da.X.min(), da.X.max()], - ylim=[da.Y.min(), da.Y.max()], - zlim=[da.Z.min(), da.Z.max()], + xlim=[X.min(), X.max()], + ylim=[Y.min(), Y.max()], + zlim=[Z.min(), Z.max()], ) color = '0.4' # color of the line of the corners From 03537f134dcbf9083ece19c4469b20062bfe9d5c Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Wed, 27 Jan 2021 12:31:17 -0500 Subject: [PATCH 08/20] Update box3d.py Incorporate flake8 suggestions --- examples/mplot3d/box3d.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index f62675224197..1bbd3f7b37af 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -16,11 +16,10 @@ import matplotlib.pyplot as plt import numpy as np -import xarray as xr # Define dimensions Nx, Ny, Nz = 100, 300, 500 -X,Y,Z = np.meshgrid(np.arange(Nx), np.arange(Ny), -np.arange(Nz)) +X, Y, Z = np.meshgrid(np.arange(Nx), np.arange(Ny), -np.arange(Nz)) # Create fake data da = (((X+100)**2 + (Y-20)**2 + 2*Z)/1000+1) @@ -40,9 +39,9 @@ ax = fig.add_subplot(111, projection='3d') # Plot contour surfaces -C = ax.contourf(X[:,:,0], Y[:,:,0], da[:,:,0], zdir='z', offset=0, **kw) -C = ax.contourf(X[0,:,:], da[0,:,:], Z[0,:,:], zdir='y', offset=0, **kw) -C = ax.contourf(da[:,-1,:], Y[:,-1,:], Z[:,-1,:], zdir='x', offset=X.max(), **kw) +C = ax.contourf(X[:, :, 0], Y[:, :, 0], da[:, :, 0], zdir='z', offset=0, **kw) +C = ax.contourf(X[0, :, :], da[0, :, :], Z[0, :, :], zdir='y', offset=0, **kw) +C = ax.contourf(da[:, -1, :], Y[:, -1, :], Z[:, -1, :], zdir='x', offset=X.max(), **kw) # -- From 3dd17cc14d6304a4968ebead1facb4fd3d01ed6f Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Wed, 27 Jan 2021 12:35:13 -0500 Subject: [PATCH 09/20] Update box3d.py Breaking long lines --- examples/mplot3d/box3d.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index 1bbd3f7b37af..c1413cb1d996 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -39,9 +39,18 @@ ax = fig.add_subplot(111, projection='3d') # Plot contour surfaces -C = ax.contourf(X[:, :, 0], Y[:, :, 0], da[:, :, 0], zdir='z', offset=0, **kw) -C = ax.contourf(X[0, :, :], da[0, :, :], Z[0, :, :], zdir='y', offset=0, **kw) -C = ax.contourf(da[:, -1, :], Y[:, -1, :], Z[:, -1, :], zdir='x', offset=X.max(), **kw) +_ = ax.contourf( + X[:, :, 0], Y[:, :, 0], da[:, :, 0], + zdir='z', offset=0, **kw +) +_ = ax.contourf( + X[0, :, :], da[0, :, :], Z[0, :, :], + zdir='y', offset=0, **kw +) +C = ax.contourf( + da[:, -1, :], Y[:, -1, :], Z[:, -1, :], + zdir='x', offset=X.max(), **kw +) # -- From e4b80fd93cbf41b8785e1fcab520f040dcea5076 Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Thu, 4 Feb 2021 18:34:53 -0500 Subject: [PATCH 10/20] Update examples/mplot3d/box3d.py Change 'da' to 'data' Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- examples/mplot3d/box3d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index c1413cb1d996..20a150946e7c 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -22,7 +22,7 @@ X, Y, Z = np.meshgrid(np.arange(Nx), np.arange(Ny), -np.arange(Nz)) # Create fake data -da = (((X+100)**2 + (Y-20)**2 + 2*Z)/1000+1) +data = (((X+100)**2 + (Y-20)**2 + 2*Z)/1000+1) vmin = da.min() vmax = da.max() From adeaa0dfcd8c922478098ec369e4d6c6d5c6f18f Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Thu, 4 Feb 2021 18:35:45 -0500 Subject: [PATCH 11/20] Update examples/mplot3d/box3d.py Simplify definition for contourf key args Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- examples/mplot3d/box3d.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index 20a150946e7c..b7423432b70d 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -24,14 +24,10 @@ # Create fake data data = (((X+100)**2 + (Y-20)**2 + 2*Z)/1000+1) -vmin = da.min() -vmax = da.max() -# Key arguments for contour plots kw = { - 'vmin': vmin, - 'vmax': vmax, - 'levels': np.linspace(vmin, vmax, 10), - 'cmap': 'viridis', + 'vmin': data.min(), + 'vmax': data.max(), + 'levels': np.linspace(data.min(), data.max(), 10), } # Create a figure with 3D ax From 34a4e98f78fea034919d8003f48fde86a0805828 Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Thu, 4 Feb 2021 18:37:24 -0500 Subject: [PATCH 12/20] Update examples/mplot3d/box3d.py Compacting code Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- examples/mplot3d/box3d.py | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index b7423432b70d..253d58c73c86 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -51,31 +51,16 @@ # Set limits of the plot from coord limits -ax.set( - xlim=[X.min(), X.max()], - ylim=[Y.min(), Y.max()], - zlim=[Z.min(), Z.max()], -) - -color = '0.4' # color of the line of the corners -# Get xlim,ylim and zlim -xlim = np.array(ax.get_xlim()) -ylim = np.array(ax.get_ylim()) -zlim = np.array(ax.get_zlim()) - -# Plot corners -ax.plot( - xlim*0+xlim[1], ylim, zlim*0, color, - linewidth=1, zorder=1e4, -) -ax.plot( - xlim, ylim*0+ylim[0], zlim*0, color, - linewidth=1, zorder=1e4, -) -ax.plot( - xlim*0+xlim[1], ylim*0+ylim[0], zlim, color, - linewidth=1, zorder=1e4, -) +xmin, xmax = X.min(), X.max() +ymin, ymax = Y.min(), Y.max() +zmin, zmax = Z.min(), Z.max() +ax.set(xlim=[xmin, xmax], ylim=[ymin, ymax], zlim=[zmin, zmax]) + +# Plot edges +edges_kw = dict(color='0.4', linewidth=1, zorder=1e3) +ax.plot([xmax, xmax], [ymin, ymax], 0, **edges_kw) +ax.plot([xmin, xmax], [ymin, ymin], 0, **edges_kw) +ax.plot([xmax, xmax], [ymin, ymin], [zmin, zmax], **edges_kw) # Set labels and zticks ax.set( From 1d6984e4c0cb195f16bb6702a975d3b845f1f345 Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Thu, 4 Feb 2021 18:38:03 -0500 Subject: [PATCH 13/20] Update examples/mplot3d/box3d.py Simplify the title of the tutorial Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- examples/mplot3d/box3d.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index 253d58c73c86..90fad68ddd23 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -1,7 +1,7 @@ """ -================================= -3D surface plot using Matplotlib -================================= +=================== +3D box surface plot +=================== by: Iury T. Simoes-Sousa (iuryt) The strategy is to select the data from each surface and plot From 1c6f9707211ed117034a06e73a59bf0d9186429f Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Thu, 4 Feb 2021 18:38:36 -0500 Subject: [PATCH 14/20] Update examples/mplot3d/box3d.py Concise documentation Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- examples/mplot3d/box3d.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index 90fad68ddd23..00c4c7baa2c1 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -4,14 +4,12 @@ =================== by: Iury T. Simoes-Sousa (iuryt) -The strategy is to select the data from each surface and plot -contours separately. -To use this feature you need to have gridded coordinates. - -The contour plot from Matplotlib has zdir argument that defines -the normal coordinate to the plotted surface. +Given data on a gridded volume ``X``, ``Y``, ``Z``, this example plots the +data values on the volume surfaces. -The offset argument defines the offset applied to the contourf surface. +The strategy is to select the data from each surface and plot +contours separately using `.Axes3D.contourf` with appropriate +parameters *zdir* and *offset*. """ import matplotlib.pyplot as plt From 3db0dd07e0edc1c35119c6169c1d03080e3e8914 Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Thu, 4 Feb 2021 18:42:13 -0500 Subject: [PATCH 15/20] Update box3d.py Remove credits --- examples/mplot3d/box3d.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index 00c4c7baa2c1..3b3fd685c044 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -2,7 +2,6 @@ =================== 3D box surface plot =================== -by: Iury T. Simoes-Sousa (iuryt) Given data on a gridded volume ``X``, ``Y``, ``Z``, this example plots the data values on the volume surfaces. From 24017e4808e41d1359298ad4976758f89eef2fde Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Thu, 4 Feb 2021 18:42:45 -0500 Subject: [PATCH 16/20] Update box3d.py Replace da for data --- examples/mplot3d/box3d.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index 3b3fd685c044..4cd66044690c 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -33,15 +33,15 @@ # Plot contour surfaces _ = ax.contourf( - X[:, :, 0], Y[:, :, 0], da[:, :, 0], + X[:, :, 0], Y[:, :, 0], data[:, :, 0], zdir='z', offset=0, **kw ) _ = ax.contourf( - X[0, :, :], da[0, :, :], Z[0, :, :], + X[0, :, :], data[0, :, :], Z[0, :, :], zdir='y', offset=0, **kw ) C = ax.contourf( - da[:, -1, :], Y[:, -1, :], Z[:, -1, :], + data[:, -1, :], Y[:, -1, :], Z[:, -1, :], zdir='x', offset=X.max(), **kw ) # -- From 68597ecffa0690e0146d59d451640b97678a993c Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Fri, 23 Apr 2021 14:18:43 -0400 Subject: [PATCH 17/20] Update examples/mplot3d/box3d.py Co-authored-by: Elliott Sales de Andrade --- examples/mplot3d/box3d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index 4cd66044690c..fa0a93a4c38e 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -7,7 +7,7 @@ data values on the volume surfaces. The strategy is to select the data from each surface and plot -contours separately using `.Axes3D.contourf` with appropriate +contours separately using `.axes3d.Axes3D.contourf` with appropriate parameters *zdir* and *offset*. """ From cf67e4f006bb94c4c05d356345bd6cc1e2249a26 Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Fri, 23 Apr 2021 14:24:25 -0400 Subject: [PATCH 18/20] Remove '\n' from xlabel and ylabel --- examples/mplot3d/box3d.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index fa0a93a4c38e..a59069e3bdc2 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -61,9 +61,9 @@ # Set labels and zticks ax.set( - xlabel='\n X [km]', - ylabel='\n Y [km]', - zlabel='\n Z [m]', + xlabel='X [km]', + ylabel='Y [km]', + zlabel='Z [m]', zticks=[0, -150, -300, -450], ) From c88ed51f30b9f3848ade088ce8e04059c542fffb Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Mon, 26 Apr 2021 00:04:49 -0400 Subject: [PATCH 19/20] Update box3d.py --- examples/mplot3d/box3d.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index a59069e3bdc2..238bdfba271d 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -73,3 +73,6 @@ # Colorbar fig.colorbar(C, ax=ax, fraction=0.02, pad=0.1, label='Name [units]') + +# Show Figure +plt.show() From 6f5f35579bc957a60275f995dc440f7cbf7a889d Mon Sep 17 00:00:00 2001 From: iury simoes-sousa Date: Mon, 3 May 2021 09:19:01 -0400 Subject: [PATCH 20/20] Update examples/mplot3d/box3d.py Co-authored-by: Elliott Sales de Andrade --- examples/mplot3d/box3d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/mplot3d/box3d.py b/examples/mplot3d/box3d.py index 238bdfba271d..7dc82248c3df 100644 --- a/examples/mplot3d/box3d.py +++ b/examples/mplot3d/box3d.py @@ -28,7 +28,7 @@ } # Create a figure with 3D ax -fig = plt.figure(figsize=(7, 4)) +fig = plt.figure(figsize=(5, 4)) ax = fig.add_subplot(111, projection='3d') # Plot contour surfaces