From 2cec00e2d382cf93e0afc3a57b098a8b466926bb Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Tue, 18 Oct 2022 13:41:54 -0400 Subject: [PATCH 01/16] Update legend.md --- doc/python/legend.md | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/doc/python/legend.md b/doc/python/legend.md index 37db5613b82..1accac76dee 100644 --- a/doc/python/legend.md +++ b/doc/python/legend.md @@ -6,7 +6,7 @@ jupyter: extension: .md format_name: markdown format_version: '1.3' - jupytext_version: 1.13.7 + jupytext_version: 1.14.1 kernelspec: display_name: Python 3 (ipykernel) language: python @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.9.0 + version: 3.8.0 plotly: description: How to configure and style the legend in Plotly with Python. display_as: file_settings @@ -185,6 +185,31 @@ fig.update_layout(legend=dict( fig.show() ``` +#### Horizontal Legend Entry Width + +*New in 5.11* + +Set the width of hozitonal legend entries by setting `entrywidth`. Here we set it to `70` pixels. Pixels is the default unit for `entrywidth`, but you can set it to be a fraction of the plot width with `entrywidthmode='fraction`. + +```python +import plotly.express as px + +df = px.data.gapminder().query("year==2007") +fig = px.scatter(df, x="gdpPercap", y="lifeExp", color="continent", + size="pop", size_max=45, log_x=True) + +fig.update_layout(legend=dict( + orientation="h", + entrywidth=70, + yanchor="bottom", + y=1.02, + xanchor="right", + x=1 +)) + +fig.show() +``` + #### Styling Legends Legends support many styling options. From 2576230b1c6240d417128d942cba78f6495c58d8 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Fri, 21 Oct 2022 14:31:54 -0400 Subject: [PATCH 02/16] add example of bounds --- doc/python/mapbox-layers.md | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/doc/python/mapbox-layers.md b/doc/python/mapbox-layers.md index 70cbaad4d7d..be73b68670f 100644 --- a/doc/python/mapbox-layers.md +++ b/doc/python/mapbox-layers.md @@ -5,10 +5,10 @@ jupyter: text_representation: extension: .md format_name: markdown - format_version: '1.2' - jupytext_version: 1.3.1 + format_version: '1.3' + jupytext_version: 1.14.1 kernelspec: - display_name: Python 3 + display_name: Python 3 (ipykernel) language: python name: python3 language_info: @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.6.8 + version: 3.8.0 plotly: description: How to make Mapbox maps in Python with various base layers, with or without needing a Mapbox Access token. @@ -192,6 +192,26 @@ fig.show() See the example in the [plotly and datashader tutorial](/python/datashader). + +#### Setting Bounds for Panning and Zooming + +Set bounds for a map to specify an area outside which a user interacting with the map can't pan or zoom. Here we +set a maximum longitude of `-180`, a minimum longitude of `-50`, a maximum latitude as `90`, and a minimum latitude of `20`. + +```python +import pandas as pd +us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv") + +import plotly.express as px + +fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"], + color_discrete_sequence=["fuchsia"], zoom=3, height=300) +fig.update_layout(mapbox_style="open-street-map") +fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) +fig.update_layout(mapbox_bounds={"west":-180, "east":-50, "south":20, "north":90}) +fig.show() +``` + #### Reference See https://plotly.com/python/reference/layout/mapbox/ for more information and options! From d739f6d9e1254de5b4481d47b5d91b4b9636dbc7 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 10:00:33 -0400 Subject: [PATCH 03/16] basic cluster example --- doc/python/scattermapbox.md | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/doc/python/scattermapbox.md b/doc/python/scattermapbox.md index 57e95452a69..7673456f177 100644 --- a/doc/python/scattermapbox.md +++ b/doc/python/scattermapbox.md @@ -5,10 +5,10 @@ jupyter: text_representation: extension: .md format_name: markdown - format_version: '1.2' - jupytext_version: 1.4.2 + format_version: '1.3' + jupytext_version: 1.14.1 kernelspec: - display_name: Python 3 + display_name: Python 3 (ipykernel) language: python name: python3 language_info: @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.7.7 + version: 3.8.0 plotly: description: How to make scatter plots on Mapbox maps in Python. display_as: maps @@ -245,6 +245,28 @@ fig.update_layout( fig.show() ``` +#### Add Clusters + +*New in 5.11* + +Display clusters of data points by setting `cluster`. Here, we enable it with `enabled=True`. Other properties available on `cluster` include `color` (for setting the color of the clusters), `size` (for setting the size of a cluster step), and `step` (for configuring how many points it takes to create a cluster or advance to the next cluster step. + + +```python +import plotly.express as px +import pandas as pd + +px.set_mapbox_access_token(open(".mapbox_token").read()) + +df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv') + +fig = px.scatter_mapbox(df, lat="lat", lon="long", size="cnt", zoom=3) + +fig.update_traces(cluster=dict(enabled=True)) + +fig.show() +``` + #### Reference See [function reference for `px.(scatter_mapbox)`](https://plotly.com/python-api-reference/generated/plotly.express.scatter_mapbox) or https://plotly.com/python/reference/scattermapbox/ for more information and options! From 93cd690576764f4e29f473e97410ab53c1d4029a Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 10:20:47 -0400 Subject: [PATCH 04/16] Update mapbox-layers.md --- doc/python/mapbox-layers.md | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/doc/python/mapbox-layers.md b/doc/python/mapbox-layers.md index be73b68670f..901f473c012 100644 --- a/doc/python/mapbox-layers.md +++ b/doc/python/mapbox-layers.md @@ -193,22 +193,32 @@ fig.show() See the example in the [plotly and datashader tutorial](/python/datashader). -#### Setting Bounds for Panning and Zooming +#### Setting Map Bounds Set bounds for a map to specify an area outside which a user interacting with the map can't pan or zoom. Here we -set a maximum longitude of `-180`, a minimum longitude of `-50`, a maximum latitude as `90`, and a minimum latitude of `20`. +set a maximum longitude of `-180`, a minimum longitude of `-50`, a maximum latitude of `90`, and a minimum latitude of `20`. ```python -import pandas as pd -us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv") - import plotly.express as px +import pandas as pd -fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"], - color_discrete_sequence=["fuchsia"], zoom=3, height=300) +us_cities = pd.read_csv( + "https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv" +) + +fig = px.scatter_mapbox( + us_cities, + lat="lat", + lon="lon", + hover_name="City", + hover_data=["State", "Population"], + color_discrete_sequence=["fuchsia"], + zoom=3, + height=300, +) fig.update_layout(mapbox_style="open-street-map") -fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) -fig.update_layout(mapbox_bounds={"west":-180, "east":-50, "south":20, "north":90}) +fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0}) +fig.update_layout(mapbox_bounds={"west": -180, "east": -50, "south": 20, "north": 90}) fig.show() ``` From 09fc1587392baa587fe708dd0d2fd6227f867c59 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 10:30:29 -0400 Subject: [PATCH 05/16] Update scattermapbox.md --- doc/python/scattermapbox.md | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/doc/python/scattermapbox.md b/doc/python/scattermapbox.md index 7673456f177..40454dbbe09 100644 --- a/doc/python/scattermapbox.md +++ b/doc/python/scattermapbox.md @@ -249,25 +249,22 @@ fig.show() *New in 5.11* -Display clusters of data points by setting `cluster`. Here, we enable it with `enabled=True`. Other properties available on `cluster` include `color` (for setting the color of the clusters), `size` (for setting the size of a cluster step), and `step` (for configuring how many points it takes to create a cluster or advance to the next cluster step. - +Display clusters of data points by setting `cluster`. Here, we enable it with `enabled=True`. You can also enable it by setting other `cluster` properties. Other available properties include `color` (for setting the color of the clusters), `size` (for setting the size of a cluster step), and `step` (for configuring how many points it takes to create a cluster or advance to the next cluster step. ```python import plotly.express as px import pandas as pd px.set_mapbox_access_token(open(".mapbox_token").read()) - -df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv') - +df = pd.read_csv( + "https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv" +) fig = px.scatter_mapbox(df, lat="lat", lon="long", size="cnt", zoom=3) - fig.update_traces(cluster=dict(enabled=True)) - fig.show() + ``` #### Reference See [function reference for `px.(scatter_mapbox)`](https://plotly.com/python-api-reference/generated/plotly.express.scatter_mapbox) or https://plotly.com/python/reference/scattermapbox/ for more information and options! - From 30bf41b8a22035f8ab8ee74df2fd42c82dbeb718 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 10:32:34 -0400 Subject: [PATCH 06/16] Update scattermapbox.md --- doc/python/scattermapbox.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/python/scattermapbox.md b/doc/python/scattermapbox.md index 40454dbbe09..05ff49c7b45 100644 --- a/doc/python/scattermapbox.md +++ b/doc/python/scattermapbox.md @@ -249,7 +249,7 @@ fig.show() *New in 5.11* -Display clusters of data points by setting `cluster`. Here, we enable it with `enabled=True`. You can also enable it by setting other `cluster` properties. Other available properties include `color` (for setting the color of the clusters), `size` (for setting the size of a cluster step), and `step` (for configuring how many points it takes to create a cluster or advance to the next cluster step. +Display clusters of data points by setting `cluster`. Here, we enable clusters with `enabled=True`. You can also enable clusters by setting other `cluster` properties. Other available properties include `color` (for setting the color of the clusters), `size` (for setting the size of a cluster step), and `step` (for configuring how many points it takes to create a cluster or advance to the next cluster step. ```python import plotly.express as px From c234bb1c0e4287d80e3e4f7aca2fd9f3aba8e7f6 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 10:33:27 -0400 Subject: [PATCH 07/16] Update mapbox-layers.md --- doc/python/mapbox-layers.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/python/mapbox-layers.md b/doc/python/mapbox-layers.md index 901f473c012..15cb44cbe34 100644 --- a/doc/python/mapbox-layers.md +++ b/doc/python/mapbox-layers.md @@ -195,8 +195,7 @@ See the example in the [plotly and datashader tutorial](/python/datashader). #### Setting Map Bounds -Set bounds for a map to specify an area outside which a user interacting with the map can't pan or zoom. Here we -set a maximum longitude of `-180`, a minimum longitude of `-50`, a maximum latitude of `90`, and a minimum latitude of `20`. +Set bounds for a map to specify an area outside which a user interacting with the map can't pan or zoom. Here we set a maximum longitude of `-180`, a minimum longitude of `-50`, a maximum latitude of `90`, and a minimum latitude of `20`. ```python import plotly.express as px From 8940b9336dfd480bbe5d170d769fc84b1638fed9 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 10:35:19 -0400 Subject: [PATCH 08/16] Update legend.md --- doc/python/legend.md | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/doc/python/legend.md b/doc/python/legend.md index f8ca4c36dd4..ee8fad23826 100644 --- a/doc/python/legend.md +++ b/doc/python/legend.md @@ -199,18 +199,20 @@ Set the width of hozitonal legend entries by setting `entrywidth`. Here we set i import plotly.express as px df = px.data.gapminder().query("year==2007") -fig = px.scatter(df, x="gdpPercap", y="lifeExp", color="continent", - size="pop", size_max=45, log_x=True) - -fig.update_layout(legend=dict( - orientation="h", - entrywidth=70, - yanchor="bottom", - y=1.02, - xanchor="right", - x=1 -)) - +fig = px.scatter( + df, + x="gdpPercap", + y="lifeExp", + color="continent", + size="pop", + size_max=45, + log_x=True, +) +fig.update_layout( + legend=dict( + orientation="h", entrywidth=70, yanchor="bottom", y=1.02, xanchor="right", x=1 + ) +) fig.show() ``` From 22492035db83e607281ca472f95dc2d7c7e28bc1 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 10:36:35 -0400 Subject: [PATCH 09/16] Update mapbox-layers.md --- doc/python/mapbox-layers.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/python/mapbox-layers.md b/doc/python/mapbox-layers.md index 15cb44cbe34..be8d238bef4 100644 --- a/doc/python/mapbox-layers.md +++ b/doc/python/mapbox-layers.md @@ -195,6 +195,8 @@ See the example in the [plotly and datashader tutorial](/python/datashader). #### Setting Map Bounds +*New in 5.11* + Set bounds for a map to specify an area outside which a user interacting with the map can't pan or zoom. Here we set a maximum longitude of `-180`, a minimum longitude of `-50`, a maximum latitude of `90`, and a minimum latitude of `20`. ```python From d7aa9e572e021868771b3185a7fd8b5da4a5852e Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 10:42:34 -0400 Subject: [PATCH 10/16] Update legend.md --- doc/python/legend.md | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/doc/python/legend.md b/doc/python/legend.md index ee8fad23826..231dab187be 100644 --- a/doc/python/legend.md +++ b/doc/python/legend.md @@ -8,7 +8,7 @@ jupyter: format_version: '1.3' jupytext_version: 1.14.1 kernelspec: - display_name: Python 3 + display_name: Python 3 (ipykernel) language: python name: python3 language_info: @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.8.8 + version: 3.8.0 plotly: description: How to configure and style the legend in Plotly with Python. display_as: file_settings @@ -199,20 +199,18 @@ Set the width of hozitonal legend entries by setting `entrywidth`. Here we set i import plotly.express as px df = px.data.gapminder().query("year==2007") -fig = px.scatter( - df, - x="gdpPercap", - y="lifeExp", - color="continent", - size="pop", - size_max=45, - log_x=True, -) -fig.update_layout( - legend=dict( - orientation="h", entrywidth=70, yanchor="bottom", y=1.02, xanchor="right", x=1 - ) -) +fig = px.scatter(df, x="gdpPercap", y="lifeExp", color="continent", + size="pop", size_max=45, log_x=True) + +fig.update_layout(legend=dict( + orientation="h", + entrywidth=70, + yanchor="bottom", + y=1.02, + xanchor="right", + x=1 +)) + fig.show() ``` From 94196a6050238cd6ee864a76a1911d9da3ad3d7f Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 10:55:03 -0400 Subject: [PATCH 11/16] Update legend.md --- doc/python/legend.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/python/legend.md b/doc/python/legend.md index 231dab187be..7fafadd107a 100644 --- a/doc/python/legend.md +++ b/doc/python/legend.md @@ -193,7 +193,7 @@ fig.show() *New in 5.11* -Set the width of hozitonal legend entries by setting `entrywidth`. Here we set it to `70` pixels. Pixels is the default unit for `entrywidth`, but you can set it to be a fraction of the plot width with `entrywidthmode='fraction`. +Set the width of hozitonal legend entries by setting `entrywidth`. Here we set it to `70` pixels. Pixels is the default unit for `entrywidth`, but you can set it to be a fraction of the plot width using `entrywidthmode='fraction`. ```python import plotly.express as px From bd896eea9949a6dd9181ed49528e71c203570589 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 11:25:59 -0400 Subject: [PATCH 12/16] Update setting-graph-size.md --- doc/python/setting-graph-size.md | 38 +++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/doc/python/setting-graph-size.md b/doc/python/setting-graph-size.md index 65a632dd37a..611e0e29825 100644 --- a/doc/python/setting-graph-size.md +++ b/doc/python/setting-graph-size.md @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.8.8 + version: 3.8.0 plotly: description: How to manipulate the graph size, margins and background color. display_as: file_settings @@ -163,6 +163,42 @@ fig.update_layout( fig.update_yaxes(automargin='left+top') +fig.show() +``` + +### Setting a Minimum Plot Size with Automargins + +*New in 5.11* + +To set a minimum width and height for a plot to be after automargin is applied, use `minreducedwidth` and `minreducedheight`. Here we set both to `250`. + +```python +import plotly.graph_objects as go + + +fig = go.Figure() + +fig.add_trace(go.Bar( + x=["Apples", "Oranges", "Watermelon", "Pears"], + y=[3, 2, 1, 4] +)) + +fig.update_layout( + autosize=False, + minreducedwidth=250, + minreducedheight=250, + width=450, + height=450, + yaxis=dict( + title_text="Y-axis Title", + ticktext=["Label", "Very long label", "Other label", "Very very long label"], + tickvals=[1, 2, 3, 4], + tickmode="array", + titlefont=dict(size=30), + ) +) + + fig.show() ``` From fded25c80fd5dda133121c52eae23d2d2985ab90 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 11:47:09 -0400 Subject: [PATCH 13/16] add marker updates --- doc/python/marker-style.md | 85 +++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 2 deletions(-) diff --git a/doc/python/marker-style.md b/doc/python/marker-style.md index e51e564bb3a..81f45306f5f 100644 --- a/doc/python/marker-style.md +++ b/doc/python/marker-style.md @@ -8,7 +8,7 @@ jupyter: format_version: '1.3' jupytext_version: 1.14.1 kernelspec: - display_name: Python 3 + display_name: Python 3 (ipykernel) language: python name: python3 language_info: @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.8.8 + version: 3.8.0 plotly: description: How to style markers in Python with Plotly. display_as: file_settings @@ -332,6 +332,8 @@ Each basic symbol is also represented by a number. Adding 100 to that number is In the following figure, hover over a symbol to see its name or number. Set the `marker_symbol` attribute equal to that name or number to change the marker symbol in your figure. +> The `arrow-wide` and `arrow` marker symbols are new in 5.11 + ```python import plotly.graph_objects as go from plotly.validators.scatter.marker import SymbolValidator @@ -357,6 +359,85 @@ fig.show() ``` +### Using a Custom Marker + +To use a custom marker, set the `symbol` on the `marker`. Here we set it to `diamond`. + + +```python +import plotly.express as px + +df = px.data.iris() +fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species") + +fig.update_traces(marker=dict(size=8, + symbol='diamond', + line=dict(width=2, + color='DarkSlateGrey')), + selector=dict(mode='markers')) +fig.show() + +``` + +### Setting Marker Angles + + +*New in 5.11* + +Change the angle of markers by setting `angle`. Here we set the angle on the `arrow` markers to `45`. + +```python +import plotly.express as px + +df = px.data.iris() +fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species") + +fig.update_traces(marker=dict(size=12, + symbol='arrow', + angle=45, + line=dict(width=2, + color='DarkSlateGrey')), + selector=dict(mode='markers')) +fig.show() + +``` + +### Setting Angle Reference + +*New in 5.11* + +In the previous example the angle reference is the default `up`, which +means all makers start at the angle reference point of 0. Set `angleref` to `previous` and a marker will take its angle reference from the previous data point. + +```python +import pandas as pd +import plotly.express as px +import plotly.graph_objects as go + + +df = px.data.gapminder() + +fig = go.Figure() + +for x in df.loc[df.continent.isin(["Europe"])].country.unique()[:5]: + fil = df.loc[(df.country.str.contains(x))] + fig.add_trace( + go.Scatter( + x=fil["year"], + y=fil["pop"], + mode="lines+markers", + marker=dict( + symbol="arrow", + size=15, + angleref="previous", + ), + name=x, + ) + ) +fig.show() + +``` + ### Reference See https://plotly.com/python/reference/ for more information and chart attribute options! From 2d3714bf59a003ffcc6cc565a0ff65dabb196ccc Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 11:48:31 -0400 Subject: [PATCH 14/16] Update marker-style.md --- doc/python/marker-style.md | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/doc/python/marker-style.md b/doc/python/marker-style.md index 81f45306f5f..49fdd756bac 100644 --- a/doc/python/marker-style.md +++ b/doc/python/marker-style.md @@ -370,11 +370,10 @@ import plotly.express as px df = px.data.iris() fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species") -fig.update_traces(marker=dict(size=8, - symbol='diamond', - line=dict(width=2, - color='DarkSlateGrey')), - selector=dict(mode='markers')) +fig.update_traces( + marker=dict(size=8, symbol="diamond", line=dict(width=2, color="DarkSlateGrey")), + selector=dict(mode="markers"), +) fig.show() ``` @@ -392,12 +391,12 @@ import plotly.express as px df = px.data.iris() fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species") -fig.update_traces(marker=dict(size=12, - symbol='arrow', - angle=45, - line=dict(width=2, - color='DarkSlateGrey')), - selector=dict(mode='markers')) +fig.update_traces( + marker=dict( + size=12, symbol="arrow", angle=45, line=dict(width=2, color="DarkSlateGrey") + ), + selector=dict(mode="markers"), +) fig.show() ``` From e909b325e96e16ce7d6356942fde7bcae7b8e4c7 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 14:57:20 -0400 Subject: [PATCH 15/16] Create dumbbell-plots.md --- doc/python/dumbbell-plots.md | 176 +++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 doc/python/dumbbell-plots.md diff --git a/doc/python/dumbbell-plots.md b/doc/python/dumbbell-plots.md new file mode 100644 index 00000000000..f3ff1f8dc51 --- /dev/null +++ b/doc/python/dumbbell-plots.md @@ -0,0 +1,176 @@ +--- +jupyter: + jupytext: + notebook_metadata_filter: all + text_representation: + extension: .md + format_name: markdown + format_version: '1.3' + jupytext_version: 1.14.1 + kernelspec: + display_name: Python 3 (ipykernel) + language: python + name: python3 + language_info: + codemirror_mode: + name: ipython + version: 3 + file_extension: .py + mimetype: text/x-python + name: python + nbconvert_exporter: python + pygments_lexer: ipython3 + version: 3.8.0 + plotly: + description: How to create dumbbell plots in Python with Plotly. + display_as: basic + language: python + layout: base + name: Dumbbell Plots + order: 19 + page_type: example_index + permalink: python/dumbbell-plots/ + thumbnail: thumbnail/dumbbell-plot.jpg +--- + +## Basic Dumbbell Plot + + +Dumbbell plots are useful for demonstrating change between two sets of data points, for example, the population change for a selection of countries for two different years + +In this example, we compare life expectancy in 1952 with life expectancy in 2002 for countries in Europe. + +```python +import plotly.graph_objects as go +from plotly import data + +import pandas as pd + +df = data.gapminder() +df = df.loc[(df.continent == "Europe") & (df.year.isin([1952, 2002]))] + +countries = ( + df.loc[(df.continent == "Europe") & (df.year.isin([2002]))] + .sort_values(by=["lifeExp"], ascending=True)["country"] + .unique() +) + +data = {"x": [], "y": [], "colors": [], "years": []} + +for country in countries: + data["x"].extend( + [ + df.loc[(df.year == 1952) & (df.country == country)]["lifeExp"].values[0], + df.loc[(df.year == 2002) & (df.country == country)]["lifeExp"].values[0], + None, + ] + ) + data["y"].extend([country, country, None]), + data["colors"].extend(["green", "blue", "brown"]), + data["years"].extend(["1952", "2002", None]) + +fig = go.Figure( + data=[ + go.Scatter( + x=data["x"], + y=data["y"], + mode="lines", + marker=dict( + color="grey", + ), + ), + go.Scatter( + x=data["x"], + y=data["y"], + mode="markers+text", + marker=dict( + color=data["colors"], + size=10, + ), + hovertemplate="""Country: %{y}
Life Expectancy: %{x}
""", + ), + ] +) + +fig.update_layout( + title="Life Expectancy in Europe: 1952 and 2002", + width=1000, + height=1000, + showlegend=False, +) + +fig.show() + +``` + +## Dumbbell Plot with Arrow Markers + +*Note: The `arrow`, `angleref`, and `standoff` properties used on the `marker` in this example are new in 5.11* + +In this example, we add arrow markers to the plot. The first trace adds the lines connecting the data points and arrow markers. +The second trace adds circle markers. On the first trace, we use `standoff=8` to position the arrow marker back from the data point. +For the arrow marker to point directly at the circle marker, this value should be half the circle marker size. + +```python +import pandas as pd +import plotly.graph_objects as go +from plotly import data + +df = data.gapminder() +df = df.loc[(df.continent == "Europe") & (df.year.isin([1952, 2002]))] + +countries = ( + df.loc[(df.continent == "Europe") & (df.year.isin([2002]))] + .sort_values(by=["lifeExp"], ascending=True)["country"] + .unique() +) + +data = {"x": [], "y": [], "colors": [], "years": []} + +for country in countries: + data["x"].extend( + [ + df.loc[(df.year == 1952) & (df.country == country)]["lifeExp"].values[0], + df.loc[(df.year == 2002) & (df.country == country)]["lifeExp"].values[0], + None, + ] + ) + data["y"].extend([country, country, None]), + data["colors"].extend(["silver", "lightskyblue", "white"]), + data["years"].extend(["1952", "2002", None]) + +fig = go.Figure( + data=[ + go.Scatter( + x=data["x"], + y=data["y"], + mode="markers+lines", + marker=dict( + symbol="arrow", color="black", size=16, angleref="previous", standoff=8 + ), + ), + go.Scatter( + x=data["x"], + y=data["y"], + text=data["years"], + mode="markers", + marker=dict( + color=data["colors"], + size=16, + ), + hovertemplate="""Country: %{y}
Life Expectancy: %{x}
Year: %{text}
""", + ), + ] +) + +fig.update_layout( + title="Life Expectancy in Europe: 1952 and 2002", + width=1000, + height=1000, + showlegend=False, +) + + +fig.show() + +``` From 79c49c157d6335c1c2dca79d4cc4c89ae1fa8f34 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 24 Oct 2022 15:32:30 -0400 Subject: [PATCH 16/16] standoff example --- doc/python/marker-style.md | 76 +++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/doc/python/marker-style.md b/doc/python/marker-style.md index 49fdd756bac..128b22d04c5 100644 --- a/doc/python/marker-style.md +++ b/doc/python/marker-style.md @@ -413,7 +413,6 @@ import pandas as pd import plotly.express as px import plotly.graph_objects as go - df = px.data.gapminder() fig = go.Figure() @@ -437,6 +436,81 @@ fig.show() ``` +### Using Standoff to Position a Marker + +*New in 5.11* + +When you have multiple markers at one location, you can use `standoff` on a marker to move it away from the other marker in the direction of the `angle`. +In this example, we set `standoff=8` on the `arrow` marker, which is half the size of the other `circle` marker, meaning it points exactly at the `circle`. + +```python +import pandas as pd +import plotly.graph_objects as go +from plotly import data + +df = data.gapminder() +df = df.loc[(df.continent == "Americas") & (df.year.isin([1987, 2007]))] + +countries = ( + df.loc[(df.continent == "Americas") & (df.year.isin([2007]))] + .sort_values(by=["pop"], ascending=True)["country"] + .unique() +)[5:-10] + +data = {"x": [], "y": [], "colors": [], "years": []} + +for country in countries: + data["x"].extend( + [ + df.loc[(df.year == 1987) & (df.country == country)]["pop"].values[0], + df.loc[(df.year == 2007) & (df.country == country)]["pop"].values[0], + None, + ] + ) + data["y"].extend([country, country, None]), + data["colors"].extend(["cyan", "darkblue", "white"]), + data["years"].extend(["1987", "2007", None]) + +fig = go.Figure( + data=[ + go.Scatter( + x=data["x"], + y=data["y"], + mode="markers+lines", + marker=dict( + symbol="arrow", + color="royalblue", + size=16, + angleref="previous", + standoff=8, + ), + ), + go.Scatter( + x=data["x"], + y=data["y"], + text=data["years"], + mode="markers", + marker=dict( + color=data["colors"], + size=16, + ), + hovertemplate="""Country: %{y}
Population: %{x}
Year: %{text}
""", + ), + ] +) + +fig.update_layout( + title="Population changes 1987 to 2007", + width=1000, + height=1000, + showlegend=False, +) + + +fig.show() + +``` + ### Reference See https://plotly.com/python/reference/ for more information and chart attribute options!