Skip to content

Add group scatter + rounded corners on treemap + multi-axis examples (next release) #3980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 12, 2023
64 changes: 59 additions & 5 deletions doc/python/graphing-multiple-chart-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -121,4 +175,4 @@ fig.show()
```

#### Reference
See https://plotly.com/python/reference/ for more information and attribute options!
See https://plotly.com/python/reference/ for more information and attribute options!
36 changes: 34 additions & 2 deletions doc/python/line-and-scatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -117,6 +117,38 @@ 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/).
Expand Down