From 24cf76f8e4a91856a5833aa194b0bb30d7702da7 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 12 Dec 2022 11:28:17 -0500 Subject: [PATCH 01/13] Add group scatter examples --- doc/python/line-and-scatter.md | 42 ++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/doc/python/line-and-scatter.md b/doc/python/line-and-scatter.md index cfbcf34730c..cd6276948ad 100644 --- a/doc/python/line-and-scatter.md +++ b/doc/python/line-and-scatter.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 make scatter plots in Python with Plotly. display_as: basic @@ -117,6 +117,44 @@ fig.update_traces(marker_size=10) fig.show() ``` +### Grouped Scatter Points + +*New in 5.12* + +By default, scatter points at the same location are overlayed on one another. We can see this in the previous example, with the values for Canada for bronze and silver. Set `scattermode='group'` to plot scatter points next to one another, centered around the shared location. + +```python +import plotly.express as px + +df = px.data.medals_long() + +fig = px.scatter(df, y="count", x="nation", color="medal") +fig.update_traces(marker_size=10) +fig.update_layout( + scattermode='group', +) +fig.show() +``` + +*New in 5.12* + +With `scattermode='group'`, a default scattergap of `0` is used. You can configure the gap between points using `scattergap`. Here we set it to `0.75` to bring the points closer together. + +```python +import plotly.express as px + +df = px.data.medals_long() + +fig = px.scatter(df, y="count", x="nation", color="medal") +fig.update_traces(marker_size=10) +fig.update_layout( + scattermode='group', + scattergap=0.75 +) + +fig.show() +``` + ### Error Bars Scatter plots support [error bars](https://plotly.com/python/error-bars/). From aebf1438acec577872e83a8014eaa42a8287a004 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 12 Dec 2022 11:35:24 -0500 Subject: [PATCH 02/13] format examples --- doc/python/line-and-scatter.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/doc/python/line-and-scatter.md b/doc/python/line-and-scatter.md index cd6276948ad..45700f15b2e 100644 --- a/doc/python/line-and-scatter.md +++ b/doc/python/line-and-scatter.md @@ -130,9 +130,7 @@ df = px.data.medals_long() fig = px.scatter(df, y="count", x="nation", color="medal") fig.update_traces(marker_size=10) -fig.update_layout( - scattermode='group', -) +fig.update_layout(scattermode="group") fig.show() ``` @@ -147,11 +145,7 @@ df = px.data.medals_long() fig = px.scatter(df, y="count", x="nation", color="medal") fig.update_traces(marker_size=10) -fig.update_layout( - scattermode='group', - scattergap=0.75 -) - +fig.update_layout(scattermode="group", scattergap=0.75) fig.show() ``` From 0e71bb1297b735b5ad03246660de7eb584371a4a Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Mon, 12 Dec 2022 13:09:01 -0500 Subject: [PATCH 03/13] Update graphing-multiple-chart-types.md --- doc/python/graphing-multiple-chart-types.md | 64 +++++++++++++++++++-- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/doc/python/graphing-multiple-chart-types.md b/doc/python/graphing-multiple-chart-types.md index 881cd75cfe3..fb72ecfc140 100644 --- a/doc/python/graphing-multiple-chart-types.md +++ b/doc/python/graphing-multiple-chart-types.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 design figures with multiple chart types in python. display_as: file_settings @@ -56,6 +56,60 @@ fig.add_bar(x=fruits, y=[2,1,3], name="Last year") fig.show() ``` +#### Grouped Bar and Scatter Chart + +*New in 5.12* + +In this example, we display individual data points with a grouped scatter chart and show grouped averages using a bar chart. We start by creating a bar chart with Plotly Express and then add scatter traces with the `add_trace()` method. + +```python +import plotly.express as px +import plotly.graph_objects as go + +df = px.data.tips()[px.data.tips()["day"] == "Sun"] + +mean_values_df = df.groupby(by=["sex", "smoker"], as_index=False).mean( + numeric_only=True +) +smoker = df[df.smoker == "Yes"].sort_values(by="tip", ascending=False) +non_smoker = df[df.smoker == "No"].sort_values(by="tip", ascending=False) + +fig = px.bar( + mean_values_df, + x="sex", + y="tip", + color="smoker", + barmode="group", + height=400, + labels={"No": "nakfdnlska"}, + color_discrete_sequence=["IndianRed", "LightSalmon"], +) + +fig.add_trace( + go.Scatter( + x=non_smoker.sex, + y=non_smoker.tip, + mode="markers", + name="No - Individual tips", + marker=dict(color="LightSteelBlue", size=5), + ) +) + +fig.add_trace( + go.Scatter( + x=smoker.sex, + y=smoker.tip, + mode="markers", + name="Yes - Individual tips", + marker=dict(color="LightSlateGrey", size=5), + ) +) + +fig.update_layout(scattermode="group") + +fig.show() +``` + #### Line Chart and a Bar Chart ```python @@ -121,4 +175,4 @@ fig.show() ``` #### Reference -See https://plotly.com/python/reference/ for more information and attribute options! \ No newline at end of file +See https://plotly.com/python/reference/ for more information and attribute options! From 4f54799571b42a5e67e4788b53f4d6b4bc78327f Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Tue, 13 Dec 2022 12:59:31 -0500 Subject: [PATCH 04/13] Update doc/python/graphing-multiple-chart-types.md --- doc/python/graphing-multiple-chart-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/python/graphing-multiple-chart-types.md b/doc/python/graphing-multiple-chart-types.md index fb72ecfc140..ffb2c6c07ef 100644 --- a/doc/python/graphing-multiple-chart-types.md +++ b/doc/python/graphing-multiple-chart-types.md @@ -60,7 +60,7 @@ fig.show() *New in 5.12* -In this example, we display individual data points with a grouped scatter chart and show grouped averages using a bar chart. We start by creating a bar chart with Plotly Express and then add scatter traces with the `add_trace()` method. +In this example, we display individual data points with a grouped scatter chart and show averages using a grouped bar chart. We start by creating a bar chart with Plotly Express and then add scatter traces with the `add_trace()` method. ```python import plotly.express as px From f03a9b4e856420e51dcbc728266530161b8afc8f Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Tue, 13 Dec 2022 14:27:49 -0500 Subject: [PATCH 05/13] Update line-and-scatter.md --- doc/python/line-and-scatter.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/python/line-and-scatter.md b/doc/python/line-and-scatter.md index 45700f15b2e..3c754bc7733 100644 --- a/doc/python/line-and-scatter.md +++ b/doc/python/line-and-scatter.md @@ -121,7 +121,7 @@ fig.show() *New in 5.12* -By default, scatter points at the same location are overlayed on one another. We can see this in the previous example, with the values for Canada for bronze and silver. Set `scattermode='group'` to plot scatter points next to one another, centered around the shared location. +By default, scatter points at the same location are overlayed. We can see this in the previous example, with the values for Canada for bronze and silver. Set `scattermode='group'` to plot scatter points next to one another, centered around the shared location. ```python import plotly.express as px @@ -136,7 +136,9 @@ fig.show() *New in 5.12* -With `scattermode='group'`, a default scattergap of `0` is used. You can configure the gap between points using `scattergap`. Here we set it to `0.75` to bring the points closer together. +You can configure the gap between points using `scattergap`. Here we set it to `0.75` to bring the points closer together. + +If you don't set `scattergap`, a default value of `0` is used, unless you have `bargap` set. If you have `bargap` set, the `scattergap` defaults to that value. ```python import plotly.express as px From 9f3368ff1e8da72c49cbf5e76e95aca394460c7b Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Thu, 22 Dec 2022 15:22:22 -0500 Subject: [PATCH 06/13] Update treemaps.md --- doc/python/treemaps.md | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/doc/python/treemaps.md b/doc/python/treemaps.md index 2bd9f3c74b0..019e29846bf 100644 --- a/doc/python/treemaps.md +++ b/doc/python/treemaps.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 Treemap Charts with Plotly display_as: basic @@ -145,6 +145,23 @@ fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) fig.show() ``` +### Treemap with Rounded Corners + + +*New in 5.12* + +Update treemap sectors to have rounded corners by configuring the `cornerradius` in px. + +```python +import plotly.express as px +fig = px.treemap( + names = ["Eve","Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"], + parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"] +) +fig.update_traces(marker={"cornerradius": 5}) +fig.show() +``` + ### Basic Treemap with go.Treemap If Plotly Express does not provide a good starting point, it is also possible to use [the more generic `go.Treemap` class from `plotly.graph_objects`](/python/graph-objects/). From b30797d3971448c129c29fff29d364f2b19e6521 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Fri, 23 Dec 2022 10:03:05 -0500 Subject: [PATCH 07/13] Update graphing-multiple-chart-types.md --- doc/python/graphing-multiple-chart-types.md | 48 +++++++++++++++------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/doc/python/graphing-multiple-chart-types.md b/doc/python/graphing-multiple-chart-types.md index ffb2c6c07ef..82d9efba488 100644 --- a/doc/python/graphing-multiple-chart-types.md +++ b/doc/python/graphing-multiple-chart-types.md @@ -60,7 +60,7 @@ fig.show() *New in 5.12* -In this example, we display individual data points with a grouped scatter chart and show averages using a grouped bar chart. We start by creating a bar chart with Plotly Express and then add scatter traces with the `add_trace()` method. +In this example, we display individual data points with a grouped scatter chart and show averages using a grouped bar chart. `offsetgroup` links the bar trace for smoker with the scatter trace for smoker, and the bar trace for non-smoker with the scatter trace for non-smoker. If you deselect a trace using the legend, other traces maintain the position of the traces they are linked to. ```python import plotly.express as px @@ -71,27 +71,49 @@ df = px.data.tips()[px.data.tips()["day"] == "Sun"] mean_values_df = df.groupby(by=["sex", "smoker"], as_index=False).mean( numeric_only=True ) + +smoker_mean = mean_values_df[mean_values_df.smoker == "Yes"].sort_values( + by="tip", ascending=False +) +non_smoker_mean = mean_values_df[mean_values_df.smoker == "No"].sort_values( + by="tip", ascending=False +) + smoker = df[df.smoker == "Yes"].sort_values(by="tip", ascending=False) non_smoker = df[df.smoker == "No"].sort_values(by="tip", ascending=False) -fig = px.bar( - mean_values_df, - x="sex", - y="tip", - color="smoker", - barmode="group", - height=400, - labels={"No": "nakfdnlska"}, - color_discrete_sequence=["IndianRed", "LightSalmon"], +fig = go.Figure() + +fig.add_trace( + go.Bar( + x=smoker_mean.sex, + y=smoker_mean.tip, + name="Average (Smoker)", + marker_color="IndianRed", + offsetgroup="smoker", + ) ) + +fig.add_trace( + go.Bar( + x=non_smoker_mean.sex, + y=non_smoker_mean.tip, + name="Average (Non-Smoker)", + marker_color="LightSalmon", + offsetgroup="non-smoker", + ) +) + + fig.add_trace( go.Scatter( x=non_smoker.sex, y=non_smoker.tip, mode="markers", - name="No - Individual tips", + name="Individual tips (Non-Smoker)", marker=dict(color="LightSteelBlue", size=5), + offsetgroup="non-smoker", ) ) @@ -100,14 +122,16 @@ fig.add_trace( x=smoker.sex, y=smoker.tip, mode="markers", - name="Yes - Individual tips", + name="Individual tips (Smoker)", marker=dict(color="LightSlateGrey", size=5), + offsetgroup="smoker", ) ) fig.update_layout(scattermode="group") fig.show() + ``` #### Line Chart and a Bar Chart From 04aeb581636d50a9fafee45cc594a9880b646154 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Fri, 23 Dec 2022 10:03:16 -0500 Subject: [PATCH 08/13] Update treemaps.md --- doc/python/treemaps.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/python/treemaps.md b/doc/python/treemaps.md index 019e29846bf..94254e9b238 100644 --- a/doc/python/treemaps.md +++ b/doc/python/treemaps.md @@ -158,7 +158,7 @@ fig = px.treemap( names = ["Eve","Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"], parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"] ) -fig.update_traces(marker={"cornerradius": 5}) +fig.update_traces(marker=dict(cornerradius=5)) fig.show() ``` From 1254d9fe91b4073a1fe1c42a74b9e1aea62bfe88 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Fri, 23 Dec 2022 10:03:22 -0500 Subject: [PATCH 09/13] Update line-and-scatter.md --- doc/python/line-and-scatter.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/python/line-and-scatter.md b/doc/python/line-and-scatter.md index 3c754bc7733..8b7c6e4082d 100644 --- a/doc/python/line-and-scatter.md +++ b/doc/python/line-and-scatter.md @@ -136,9 +136,8 @@ fig.show() *New in 5.12* -You can configure the gap between points using `scattergap`. Here we set it to `0.75` to bring the points closer together. +You can configure the gap between groups of scatter points using `scattergap`. Here we set it to `0.75`, which brings the points closer together by allocating more space to the gap between groups. If you don't set `scattergap`, a default value of `0` is used, unless you have `bargap` set. If you have `bargap` set, the `scattergap` defaults to that value. -If you don't set `scattergap`, a default value of `0` is used, unless you have `bargap` set. If you have `bargap` set, the `scattergap` defaults to that value. ```python import plotly.express as px From ac0831b8b81994b9518b6679b5431f82c299d8b2 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Fri, 23 Dec 2022 15:08:30 -0500 Subject: [PATCH 10/13] Update multiple-axes.md --- doc/python/multiple-axes.md | 105 +++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-) diff --git a/doc/python/multiple-axes.md b/doc/python/multiple-axes.md index 389c6a1a14b..66f1dceb0ea 100644 --- a/doc/python/multiple-axes.md +++ b/doc/python/multiple-axes.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 make a graph with multiple axes (dual y-axis plots, plots with secondary axes) in python. @@ -249,5 +249,106 @@ fig.update_layout( fig.show() ``` +#### Automatically Shifting Axes + +To automatically reposition axes to avoid overlap with other axes with the same `overlaying` value, set `autoshift=True`. For `autoshift` to work on an axis, you'll also need to set `anchor="free"` on that axis. + +```python +import plotly.graph_objects as go + +fig = go.Figure() + +fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="yaxis data")) + + +fig.add_trace(go.Scatter(x=[2, 3, 4], y=[40, 50, 60], name="yaxis2 data", yaxis="y2")) + +fig.add_trace( + go.Scatter(x=[4, 5, 6], y=[1000, 2000, 3000], name="yaxis3 data", yaxis="y3") +) + +fig.add_trace( + go.Scatter(x=[3, 4, 5], y=[400, 500, 600], name="yaxis4 data", yaxis="y4") +) + + +fig.update_layout( + xaxis=dict(domain=[0.25, 0.75]), + yaxis=dict( + title="yaxis title", + ), + yaxis2=dict( + title="yaxis2 title", + overlaying="y", + side="right", + ), + yaxis3=dict(title="yaxis3 title", anchor="free", overlaying="y", autoshift=True), + yaxis4=dict( + title="yaxis4 title", + anchor="free", + overlaying="y", + autoshift=True, + ), +) + +fig.update_layout( + title_text="Shifting y-axes with autoshift", +) + +fig.show() + +``` + +### Shift Axes by a Specific Number of Pixels + +Set a `shift` value on an axis to shift an axis by that number of pixels. A positive value shifts an axis to the right. A negative value shifts it to the left. Here, we shift `yaxis4` 100 pixels further to the left. + +```python +import plotly.graph_objects as go + +fig = go.Figure() + +fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="yaxis data")) + + +fig.add_trace(go.Scatter(x=[2, 3, 4], y=[40, 50, 60], name="yaxis2 data", yaxis="y2")) + +fig.add_trace( + go.Scatter(x=[4, 5, 6], y=[1000, 2000, 3000], name="yaxis3 data", yaxis="y3") +) + +fig.add_trace( + go.Scatter(x=[3, 4, 5], y=[400, 500, 600], name="yaxis4 data", yaxis="y4") +) + + +fig.update_layout( + xaxis=dict(domain=[0.25, 0.75]), + yaxis=dict( + title="yaxis title", + ), + yaxis2=dict( + title="yaxis2 title", + overlaying="y", + side="right", + ), + yaxis3=dict(title="yaxis3 title", anchor="free", overlaying="y", autoshift=True), + yaxis4=dict( + title="yaxis4 title", + anchor="free", + overlaying="y", + autoshift=True, + shift=-100, + ), +) + +fig.update_layout( + title_text="Shifting y-axes by a specific number of pixels", +) + +fig.show() + +``` + #### Reference All of the y-axis properties are found here: https://plotly.com/python/reference/YAxis/. For more information on creating subplots see the [Subplots in Python](/python/subplots/) section. From 1a182ff52c9b076bed9060d10d3074a7d34c08d9 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Fri, 23 Dec 2022 15:25:11 -0500 Subject: [PATCH 11/13] Update multiple-axes.md --- doc/python/multiple-axes.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/python/multiple-axes.md b/doc/python/multiple-axes.md index 66f1dceb0ea..db444b4ec6f 100644 --- a/doc/python/multiple-axes.md +++ b/doc/python/multiple-axes.md @@ -251,6 +251,8 @@ fig.show() #### Automatically Shifting Axes +*New in 5.12* + To automatically reposition axes to avoid overlap with other axes with the same `overlaying` value, set `autoshift=True`. For `autoshift` to work on an axis, you'll also need to set `anchor="free"` on that axis. ```python @@ -301,6 +303,8 @@ fig.show() ### Shift Axes by a Specific Number of Pixels +*New in 5.12* + Set a `shift` value on an axis to shift an axis by that number of pixels. A positive value shifts an axis to the right. A negative value shifts it to the left. Here, we shift `yaxis4` 100 pixels further to the left. ```python From c391a3310ef8260975a87e9728c7ffc6b80269eb Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Fri, 23 Dec 2022 15:26:59 -0500 Subject: [PATCH 12/13] remove px import --- doc/python/graphing-multiple-chart-types.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/python/graphing-multiple-chart-types.md b/doc/python/graphing-multiple-chart-types.md index 82d9efba488..690bb87b12d 100644 --- a/doc/python/graphing-multiple-chart-types.md +++ b/doc/python/graphing-multiple-chart-types.md @@ -63,10 +63,10 @@ fig.show() In this example, we display individual data points with a grouped scatter chart and show averages using a grouped bar chart. `offsetgroup` links the bar trace for smoker with the scatter trace for smoker, and the bar trace for non-smoker with the scatter trace for non-smoker. If you deselect a trace using the legend, other traces maintain the position of the traces they are linked to. ```python -import plotly.express as px import plotly.graph_objects as go +from plotly import data -df = px.data.tips()[px.data.tips()["day"] == "Sun"] +df = data.tips()[data.tips()["day"] == "Sun"] mean_values_df = df.groupby(by=["sex", "smoker"], as_index=False).mean( numeric_only=True From 4db8474031df1f3e5243b08327b5a6c10070362e Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Fri, 23 Dec 2022 15:30:26 -0500 Subject: [PATCH 13/13] Update multiple-axes.md --- doc/python/multiple-axes.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/doc/python/multiple-axes.md b/doc/python/multiple-axes.md index db444b4ec6f..22974ca734f 100644 --- a/doc/python/multiple-axes.md +++ b/doc/python/multiple-axes.md @@ -262,7 +262,6 @@ fig = go.Figure() fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="yaxis data")) - fig.add_trace(go.Scatter(x=[2, 3, 4], y=[40, 50, 60], name="yaxis2 data", yaxis="y2")) fig.add_trace( @@ -314,7 +313,6 @@ fig = go.Figure() fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="yaxis data")) - fig.add_trace(go.Scatter(x=[2, 3, 4], y=[40, 50, 60], name="yaxis2 data", yaxis="y2")) fig.add_trace(