From 5d6c480f4634f332a28a1d198f1122c945f64c71 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Mon, 10 May 2021 08:43:45 +0200 Subject: [PATCH 1/9] WIP: Add categorical colorbars for plot and plot3d gallery examples As discussed in #1259 this PR fixes issues appearing during cpt creation and adds categorical colorbars to the examples. --- examples/gallery/symbols/points_categorical.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/symbols/points_categorical.py b/examples/gallery/symbols/points_categorical.py index 3abf829e7ff..72e00d47723 100644 --- a/examples/gallery/symbols/points_categorical.py +++ b/examples/gallery/symbols/points_categorical.py @@ -46,7 +46,7 @@ # new discrete CPT using series=(lowest_value, highest_value, interval), # use color_model="+c" to write the discrete color palette "inferno" in # categorical format -pygmt.makecpt(cmap="inferno", series=(0, 3, 1), color_model="+c") +pygmt.makecpt(cmap="inferno", series=(0, 2, 1), color_model="+c") fig.plot( # Use bill length and bill depth as x and y data input, respectively From e06fd9e026099f2ad32890d27a02f8df82ae765a Mon Sep 17 00:00:00 2001 From: Michael Grund Date: Mon, 10 May 2021 17:43:01 +0200 Subject: [PATCH 2/9] add colorbar --- examples/gallery/symbols/points_categorical.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/examples/gallery/symbols/points_categorical.py b/examples/gallery/symbols/points_categorical.py index 72e00d47723..e0fa8de2e17 100644 --- a/examples/gallery/symbols/points_categorical.py +++ b/examples/gallery/symbols/points_categorical.py @@ -14,8 +14,13 @@ import pandas as pd import pygmt -# Load sample penguins data and convert 'species' column to categorical dtype +# Load sample penguins data df = pd.read_csv("https://github.com/mwaskom/seaborn-data/raw/master/penguins.csv") + +# Extract species names for colorbar legend +species = ",".join(df.species.unique()) + +# Convert 'species' column to categorical dtype df.species = df.species.astype(dtype="category") # Use pygmt.info to get region bounds (xmin, xmax, ymin, ymax) @@ -44,9 +49,9 @@ # Define a colormap to be used for three categories, define the range of the # new discrete CPT using series=(lowest_value, highest_value, interval), -# use color_model="+c" to write the discrete color palette "inferno" in -# categorical format -pygmt.makecpt(cmap="inferno", series=(0, 2, 1), color_model="+c") +# use color_model="+c" + species to write the discrete color palette "inferno" in +# categorical format and add the species names extracted above as annotations +pygmt.makecpt(cmap="inferno", series=(0, 2, 1), color_model="+c" + species) fig.plot( # Use bill length and bill depth as x and y data input, respectively @@ -66,7 +71,7 @@ transparency=40, ) -# A colorbar displaying the different penguin species types will be added -# once GMT 6.2.0 is released. +# Add colorbar legend +fig.colorbar() fig.show() From eb80fbee50cb933167d27b4fe819e42dbca1f984 Mon Sep 17 00:00:00 2001 From: Michael Grund Date: Mon, 10 May 2021 17:50:52 +0200 Subject: [PATCH 3/9] update scatter3d example --- examples/gallery/3d_plots/scatter3d.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/examples/gallery/3d_plots/scatter3d.py b/examples/gallery/3d_plots/scatter3d.py index 0c70581fe32..278e9885572 100644 --- a/examples/gallery/3d_plots/scatter3d.py +++ b/examples/gallery/3d_plots/scatter3d.py @@ -17,8 +17,13 @@ import pandas as pd import pygmt -# Load sample iris data, and convert 'species' column to categorical dtype +# Load sample iris data df = pd.read_csv("https://github.com/mwaskom/seaborn-data/raw/master/iris.csv") + +# Extract species names for colorbar legend +species = ",".join(df.species.unique()) + +# Convert 'species' column to categorical dtype df.species = df.species.astype(dtype="category") # Use pygmt.info to get region bounds (xmin, xmax, ymin, ymax, zmin, zmax) @@ -36,9 +41,9 @@ # Define a colormap to be used for three categories, define the range of the # new discrete CPT using series=(lowest_value, highest_value, interval), -# use color_model="+c" to write the discrete color palette "cubhelix" in -# categorical format -pygmt.makecpt(cmap="cubhelix", color_model="+c", series=(0, 3, 1)) +# use color_model="+c" + species to write the discrete color palette "cubhelix" in +# categorical format and add the species names extracted above as annotations +pygmt.makecpt(cmap="cubhelix", color_model="+c" + species, series=(0, 2, 1)) fig.plot3d( # Use petal width, sepal length and petal length as x, y and z data input, @@ -68,4 +73,8 @@ # Vertical exaggeration factor zscale=1.5, ) + +# Add colorbar legend +fig.colorbar() + fig.show() From 1790cbd13949a8f7775588c4a2f536b75b36cd07 Mon Sep 17 00:00:00 2001 From: Michael Grund Date: Mon, 10 May 2021 19:10:46 +0200 Subject: [PATCH 4/9] update custom lines example --- examples/gallery/lines/line_custom_cpt.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/gallery/lines/line_custom_cpt.py b/examples/gallery/lines/line_custom_cpt.py index bb42c9e54e3..c52d9d53a77 100644 --- a/examples/gallery/lines/line_custom_cpt.py +++ b/examples/gallery/lines/line_custom_cpt.py @@ -21,8 +21,12 @@ fig = pygmt.Figure() fig.basemap(frame=["WSne", "af"], region=[20, 30, -10, 10]) -# Create a custom CPT with the batlow CPT and 10 discrete z-values (colors) -pygmt.makecpt(cmap="batlow", series=[0, 10, 1]) +# Create a custom CPT with the batlow CPT and 10 discrete z-values (colors), +# use color_model="+c" + ','.join(map(str, range(1,11))) to write the color +# palette in categorical format and add labels (1) to (10) for the colorbar legend +pygmt.makecpt( + cmap="batlow", series=[0, 9, 1], color_model="+c" + ",".join(map(str, range(1, 11))) +) # Plot 10 lines and set a different z-value for each line for zvalue in range(0, 10): From 60ade1c4cf1fa750e42ba1793116c14831b3cfa9 Mon Sep 17 00:00:00 2001 From: Michael Grund Date: Tue, 11 May 2021 17:10:48 +0200 Subject: [PATCH 5/9] updates based on code review --- examples/gallery/3d_plots/scatter3d.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/examples/gallery/3d_plots/scatter3d.py b/examples/gallery/3d_plots/scatter3d.py index 278e9885572..4259f2b108f 100644 --- a/examples/gallery/3d_plots/scatter3d.py +++ b/examples/gallery/3d_plots/scatter3d.py @@ -17,13 +17,8 @@ import pandas as pd import pygmt -# Load sample iris data +# Load sample iris data and convert 'species' column to categorical dtype df = pd.read_csv("https://github.com/mwaskom/seaborn-data/raw/master/iris.csv") - -# Extract species names for colorbar legend -species = ",".join(df.species.unique()) - -# Convert 'species' column to categorical dtype df.species = df.species.astype(dtype="category") # Use pygmt.info to get region bounds (xmin, xmax, ymin, ymax, zmin, zmax) @@ -41,9 +36,11 @@ # Define a colormap to be used for three categories, define the range of the # new discrete CPT using series=(lowest_value, highest_value, interval), -# use color_model="+c" + species to write the discrete color palette "cubhelix" in -# categorical format and add the species names extracted above as annotations -pygmt.makecpt(cmap="cubhelix", color_model="+c" + species, series=(0, 2, 1)) +# use color_model="+csetosa,versicolor,virginica" to write the discrete color palette +# "cubhelix" in categorical format and add the species names as annotations +pygmt.makecpt( + cmap="cubhelix", color_model="+csetosa,versicolor,virginica", series=(0, 2, 1) +) fig.plot3d( # Use petal width, sepal length and petal length as x, y and z data input, @@ -75,6 +72,6 @@ ) # Add colorbar legend -fig.colorbar() +fig.colorbar(xshift=3.1) fig.show() From 1e9c53ecdd93646b5662775f5be4ccd26b44988c Mon Sep 17 00:00:00 2001 From: Michael Grund Date: Tue, 11 May 2021 17:14:34 +0200 Subject: [PATCH 6/9] updates based on code review --- examples/gallery/3d_plots/scatter3d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/3d_plots/scatter3d.py b/examples/gallery/3d_plots/scatter3d.py index 4259f2b108f..db2f3ed7c58 100644 --- a/examples/gallery/3d_plots/scatter3d.py +++ b/examples/gallery/3d_plots/scatter3d.py @@ -60,7 +60,7 @@ region=region, # Set frame parameters frame=[ - "WsNeZ3", # z axis label positioned on 3rd corner + 'WsNeZ3+t"Iris flower data set"', # z axis label positioned on 3rd corner, add title 'xafg+l"Petal Width"', 'yafg+l"Sepal Length"', 'zafg+l"Petal Length"', From 8ed28a47a6abd1292309682c52d7155ba884796d Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Wed, 12 May 2021 08:20:51 +0200 Subject: [PATCH 7/9] Update examples/gallery/lines/line_custom_cpt.py Co-authored-by: Dongdong Tian --- examples/gallery/lines/line_custom_cpt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/lines/line_custom_cpt.py b/examples/gallery/lines/line_custom_cpt.py index c52d9d53a77..02e88842bfe 100644 --- a/examples/gallery/lines/line_custom_cpt.py +++ b/examples/gallery/lines/line_custom_cpt.py @@ -25,7 +25,7 @@ # use color_model="+c" + ','.join(map(str, range(1,11))) to write the color # palette in categorical format and add labels (1) to (10) for the colorbar legend pygmt.makecpt( - cmap="batlow", series=[0, 9, 1], color_model="+c" + ",".join(map(str, range(1, 11))) + cmap="batlow", series=[0, 9, 1], color_model="+c0-9") ) # Plot 10 lines and set a different z-value for each line From dd0b544f43532a2afa1fe15008815477b8dba7e1 Mon Sep 17 00:00:00 2001 From: Michael Grund Date: Wed, 12 May 2021 08:34:45 +0200 Subject: [PATCH 8/9] updates based on code review --- examples/gallery/3d_plots/scatter3d.py | 3 ++- examples/gallery/lines/line_custom_cpt.py | 9 ++++----- examples/gallery/symbols/points_categorical.py | 14 +++++--------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/examples/gallery/3d_plots/scatter3d.py b/examples/gallery/3d_plots/scatter3d.py index db2f3ed7c58..3c57d95b770 100644 --- a/examples/gallery/3d_plots/scatter3d.py +++ b/examples/gallery/3d_plots/scatter3d.py @@ -37,7 +37,8 @@ # Define a colormap to be used for three categories, define the range of the # new discrete CPT using series=(lowest_value, highest_value, interval), # use color_model="+csetosa,versicolor,virginica" to write the discrete color palette -# "cubhelix" in categorical format and add the species names as annotations +# "cubhelix" in categorical format and add the species names as annotations for the +# colorbar pygmt.makecpt( cmap="cubhelix", color_model="+csetosa,versicolor,virginica", series=(0, 2, 1) ) diff --git a/examples/gallery/lines/line_custom_cpt.py b/examples/gallery/lines/line_custom_cpt.py index 02e88842bfe..756aef38b7a 100644 --- a/examples/gallery/lines/line_custom_cpt.py +++ b/examples/gallery/lines/line_custom_cpt.py @@ -22,16 +22,15 @@ fig.basemap(frame=["WSne", "af"], region=[20, 30, -10, 10]) # Create a custom CPT with the batlow CPT and 10 discrete z-values (colors), -# use color_model="+c" + ','.join(map(str, range(1,11))) to write the color -# palette in categorical format and add labels (1) to (10) for the colorbar legend -pygmt.makecpt( - cmap="batlow", series=[0, 9, 1], color_model="+c0-9") -) +# use color_model="+c0-9" to write the color palette in categorical format and +# add labels (0) to (9) for the colorbar legend +pygmt.makecpt(cmap="batlow", series=[0, 9, 1], color_model="+c0-9") # Plot 10 lines and set a different z-value for each line for zvalue in range(0, 10): y = zvalue * np.sin(x) fig.plot(x=x, y=y, cmap=True, zvalue=zvalue, pen="thick,+z,-") + # Color bar to show the custom CPT and the associated z-values fig.colorbar() fig.show() diff --git a/examples/gallery/symbols/points_categorical.py b/examples/gallery/symbols/points_categorical.py index e0fa8de2e17..de5ad85a36c 100644 --- a/examples/gallery/symbols/points_categorical.py +++ b/examples/gallery/symbols/points_categorical.py @@ -14,13 +14,8 @@ import pandas as pd import pygmt -# Load sample penguins data +# Load sample penguins data and convert 'species' column to categorical dtype df = pd.read_csv("https://github.com/mwaskom/seaborn-data/raw/master/penguins.csv") - -# Extract species names for colorbar legend -species = ",".join(df.species.unique()) - -# Convert 'species' column to categorical dtype df.species = df.species.astype(dtype="category") # Use pygmt.info to get region bounds (xmin, xmax, ymin, ymax) @@ -49,9 +44,10 @@ # Define a colormap to be used for three categories, define the range of the # new discrete CPT using series=(lowest_value, highest_value, interval), -# use color_model="+c" + species to write the discrete color palette "inferno" in -# categorical format and add the species names extracted above as annotations -pygmt.makecpt(cmap="inferno", series=(0, 2, 1), color_model="+c" + species) +# use color_model="+cAdelie,Chinstrap,Gentoo" to write the discrete color palette +# "inferno" in categorical format and add the species names as annotations for the +# colorbar +pygmt.makecpt(cmap="inferno", series=(0, 2, 1), color_model="+cAdelie,Chinstrap,Gentoo") fig.plot( # Use bill length and bill depth as x and y data input, respectively From b46966bec78d5daa0206635a2f16d47530c9c30e Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Thu, 13 May 2021 10:20:55 +0200 Subject: [PATCH 9/9] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- examples/gallery/3d_plots/scatter3d.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/gallery/3d_plots/scatter3d.py b/examples/gallery/3d_plots/scatter3d.py index 3c57d95b770..d094d25f43b 100644 --- a/examples/gallery/3d_plots/scatter3d.py +++ b/examples/gallery/3d_plots/scatter3d.py @@ -36,11 +36,11 @@ # Define a colormap to be used for three categories, define the range of the # new discrete CPT using series=(lowest_value, highest_value, interval), -# use color_model="+csetosa,versicolor,virginica" to write the discrete color palette +# use color_model="+cSetosa,Versicolor,Virginica" to write the discrete color palette # "cubhelix" in categorical format and add the species names as annotations for the # colorbar pygmt.makecpt( - cmap="cubhelix", color_model="+csetosa,versicolor,virginica", series=(0, 2, 1) + cmap="cubhelix", color_model="+cSetosa,Versicolor,Virginica", series=(0, 2, 1) ) fig.plot3d( @@ -62,9 +62,9 @@ # Set frame parameters frame=[ 'WsNeZ3+t"Iris flower data set"', # z axis label positioned on 3rd corner, add title - 'xafg+l"Petal Width"', - 'yafg+l"Sepal Length"', - 'zafg+l"Petal Length"', + 'xafg+l"Petal Width (cm)"', + 'yafg+l"Sepal Length (cm)"', + 'zafg+l"Petal Length (cm)"', ], # Set perspective to azimuth NorthWest (315°), at elevation 25° perspective=[315, 25],