You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: How to make funnel charts in Python with Plotly.
16
26
display_as: financial
@@ -24,27 +34,16 @@ jupyter:
24
34
permalink: python/funnel-charts/
25
35
thumbnail: thumbnail/funnel-chart.jpg
26
36
title: Python Funnel Charts | plotly
37
+
v4upgrade: true
27
38
---
28
39
29
-
#### New to Plotly?
30
-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
31
-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
32
-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
33
-
#### Version Check
34
-
Plotly's Python API is updated frequently. Run `pip install plotly --upgrade` to update your Plotly version.
35
-
36
-
```python
37
-
import plotly
38
-
plotly.__version__
39
-
```
40
-
41
40
#### Funnel Chart Outline
42
41
43
42
*Funnel charts* are often used to represent data in different stages of a business process.
44
43
It’s an important mechanism in Business Intelligence to identify potential problem areas of a process. For example, it’s used to observe the revenue or loss in a sales process for each stage. A funnel chart has multiple *phases* and *values* associated with them. Here is a table that represents a *user flow* funnel for a social media campaign. The column named 'Values' represents the total number of users at that *Phase*.
A funnel section will be drawn using [Plotly shapes](https://plot.ly/python/shapes/), in the shape of a *Rectangle* or *Isosceles Trapezoid* depending on the value of the next phase. The phase having *maximum value* will have the width equal to the plot.
path ='M {0}{1} L {2}{3} L -{2}{3} L -{0}{1} Z'.format(*points)
113
108
114
-
shape = {
115
-
'type': 'path',
116
-
'path': path,
117
-
'fillcolor': colors[i],
118
-
'line': {
119
-
'width': 1,
120
-
'color': colors[i]
121
-
}
122
-
}
109
+
shape =dict(
110
+
type='path',
111
+
path=path,
112
+
fillcolor=colors[i],
113
+
line=dict(width=1, color=colors[i]))
123
114
shapes.append(shape)
124
115
125
116
# Y-axis location for this section's details (text)
@@ -131,8 +122,12 @@ for i in range(n_phase):
131
122
To draw the phase names and values, we are using the *text* mode in *scatter* plots. To style the plot, we are changing the background color of the plot and the plot paper, hiding the *legend* and *tick labels*, and removing the *zeroline*.
132
123
133
124
```python
125
+
from plotly import graph_objects as go
126
+
127
+
fig = go.Figure()
128
+
134
129
# For phase names
135
-
label_trace =go.Scatter(
130
+
fig.add_trace(go.Scatter(
136
131
x=[-350]*n_phase,
137
132
y=label_y,
138
133
mode='text',
@@ -141,10 +136,10 @@ label_trace = go.Scatter(
141
136
color='rgb(200,200,200)',
142
137
size=15
143
138
)
144
-
)
139
+
))
145
140
146
141
# For phase values
147
-
value_trace =go.Scatter(
142
+
fig.add_trace(go.Scatter(
148
143
x=[350]*n_phase,
149
144
y=label_y,
150
145
mode='text',
@@ -153,46 +148,44 @@ value_trace = go.Scatter(
153
148
color='rgb(200,200,200)',
154
149
size=15
155
150
)
156
-
)
151
+
))
157
152
158
-
data = [label_trace, value_trace]
159
-
160
-
layout = go.Layout(
153
+
fig.update_layout(
161
154
title="<b>Funnel Chart</b>",
162
155
titlefont=dict(
163
156
size=20,
164
157
color='rgb(203,203,203)'
165
158
),
166
-
shapes=shapes,
159
+
shapes=shapes,# funnel geometry goes here
167
160
height=560,
168
161
width=800,
169
162
showlegend=False,
170
-
paper_bgcolor='rgba(44,58,71,1)',
171
-
plot_bgcolor='rgba(44,58,71,1)',
163
+
paper_bgcolor='rgba(44,58,71,1)',
164
+
plot_bgcolor='rgba(44,58,71,1)',
172
165
xaxis=dict(
173
166
showticklabels=False,
174
167
zeroline=False,
168
+
showgrid=False
175
169
),
176
170
yaxis=dict(
177
171
showticklabels=False,
178
-
zeroline=False
172
+
zeroline=False,
173
+
showgrid=False
179
174
)
180
175
)
181
176
182
-
fig = go.Figure(data=data, layout=layout)
183
-
py.iplot(fig)
177
+
fig.show()
184
178
```
185
179
186
180
#### Segmented Funnel Chart
187
181
Instead of having a single source of data like the *funnel charts*, the segmented funnel charts have multiple data sources.
0 commit comments