Skip to content

Commit 0e71bb1

Browse files
committed
Update graphing-multiple-chart-types.md
1 parent aebf143 commit 0e71bb1

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

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

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.2'
9-
jupytext_version: 1.4.2
8+
format_version: '1.3'
9+
jupytext_version: 1.14.1
1010
kernelspec:
11-
display_name: Python 3
11+
display_name: Python 3 (ipykernel)
1212
language: python
1313
name: python3
1414
language_info:
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.7
23+
version: 3.8.0
2424
plotly:
2525
description: How to design figures with multiple chart types in python.
2626
display_as: file_settings
@@ -56,6 +56,60 @@ fig.add_bar(x=fruits, y=[2,1,3], name="Last year")
5656
fig.show()
5757
```
5858

59+
#### Grouped Bar and Scatter Chart
60+
61+
*New in 5.12*
62+
63+
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.
64+
65+
```python
66+
import plotly.express as px
67+
import plotly.graph_objects as go
68+
69+
df = px.data.tips()[px.data.tips()["day"] == "Sun"]
70+
71+
mean_values_df = df.groupby(by=["sex", "smoker"], as_index=False).mean(
72+
numeric_only=True
73+
)
74+
smoker = df[df.smoker == "Yes"].sort_values(by="tip", ascending=False)
75+
non_smoker = df[df.smoker == "No"].sort_values(by="tip", ascending=False)
76+
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"],
86+
)
87+
88+
fig.add_trace(
89+
go.Scatter(
90+
x=non_smoker.sex,
91+
y=non_smoker.tip,
92+
mode="markers",
93+
name="No - Individual tips",
94+
marker=dict(color="LightSteelBlue", size=5),
95+
)
96+
)
97+
98+
fig.add_trace(
99+
go.Scatter(
100+
x=smoker.sex,
101+
y=smoker.tip,
102+
mode="markers",
103+
name="Yes - Individual tips",
104+
marker=dict(color="LightSlateGrey", size=5),
105+
)
106+
)
107+
108+
fig.update_layout(scattermode="group")
109+
110+
fig.show()
111+
```
112+
59113
#### Line Chart and a Bar Chart
60114

61115
```python
@@ -121,4 +175,4 @@ fig.show()
121175
```
122176

123177
#### Reference
124-
See https://plotly.com/python/reference/ for more information and attribute options!
178+
See https://plotly.com/python/reference/ for more information and attribute options!

0 commit comments

Comments
 (0)