Skip to content

Commit b30797d

Browse files
committed
Update graphing-multiple-chart-types.md
1 parent 9f3368f commit b30797d

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

doc/python/graphing-multiple-chart-types.md

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fig.show()
6060

6161
*New in 5.12*
6262

63-
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.
63+
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.
6464

6565
```python
6666
import plotly.express as px
@@ -71,27 +71,49 @@ df = px.data.tips()[px.data.tips()["day"] == "Sun"]
7171
mean_values_df = df.groupby(by=["sex", "smoker"], as_index=False).mean(
7272
numeric_only=True
7373
)
74+
75+
smoker_mean = mean_values_df[mean_values_df.smoker == "Yes"].sort_values(
76+
by="tip", ascending=False
77+
)
78+
non_smoker_mean = mean_values_df[mean_values_df.smoker == "No"].sort_values(
79+
by="tip", ascending=False
80+
)
81+
7482
smoker = df[df.smoker == "Yes"].sort_values(by="tip", ascending=False)
7583
non_smoker = df[df.smoker == "No"].sort_values(by="tip", ascending=False)
7684

77-
fig = px.bar(
78-
mean_values_df,
79-
x="sex",
80-
y="tip",
81-
color="smoker",
82-
barmode="group",
83-
height=400,
84-
labels={"No": "nakfdnlska"},
85-
color_discrete_sequence=["IndianRed", "LightSalmon"],
85+
fig = go.Figure()
86+
87+
fig.add_trace(
88+
go.Bar(
89+
x=smoker_mean.sex,
90+
y=smoker_mean.tip,
91+
name="Average (Smoker)",
92+
marker_color="IndianRed",
93+
offsetgroup="smoker",
94+
)
8695
)
8796

97+
98+
fig.add_trace(
99+
go.Bar(
100+
x=non_smoker_mean.sex,
101+
y=non_smoker_mean.tip,
102+
name="Average (Non-Smoker)",
103+
marker_color="LightSalmon",
104+
offsetgroup="non-smoker",
105+
)
106+
)
107+
108+
88109
fig.add_trace(
89110
go.Scatter(
90111
x=non_smoker.sex,
91112
y=non_smoker.tip,
92113
mode="markers",
93-
name="No - Individual tips",
114+
name="Individual tips (Non-Smoker)",
94115
marker=dict(color="LightSteelBlue", size=5),
116+
offsetgroup="non-smoker",
95117
)
96118
)
97119

@@ -100,14 +122,16 @@ fig.add_trace(
100122
x=smoker.sex,
101123
y=smoker.tip,
102124
mode="markers",
103-
name="Yes - Individual tips",
125+
name="Individual tips (Smoker)",
104126
marker=dict(color="LightSlateGrey", size=5),
127+
offsetgroup="smoker",
105128
)
106129
)
107130

108131
fig.update_layout(scattermode="group")
109132

110133
fig.show()
134+
111135
```
112136

113137
#### Line Chart and a Bar Chart

0 commit comments

Comments
 (0)