Skip to content
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

Python plotly funnel #5298

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions content/plotly/concepts/express/terms/funnel/funnel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
Title: '.funnel()'
Description: 'Generates a funnel chart that visualizes the reduction of data in progressive stages.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Data'
- 'Graphs'
- 'Libraries'
- 'Methods'
- 'Plotly'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
- 'paths/data-science'
- 'paths/data-science-foundations'
---

The **.funnel()** method in Plotly Express creates a chart showing the progressive reduction, or "funneling," of data as it moves through sequential stages. The chart is composed of stacked horizontal bars, with the length of each bar representing a value at each stage in the process. This helps visualize how the values change as they move through each stage and can highlight bottlenecks or drop-offs in values.
ebikatsudon marked this conversation as resolved.
Show resolved Hide resolved

## Syntax

```pseudo
plotly.express.funnel(data_frame=None, x=None, y=None, color=None, facet_row=None, facet_col=None, ...)
```

- `data_frame`: The dataset (typically a [Pandas dataframe](https://www.codecademy.com/resources/docs/pandas/dataframe)) to be plotted. If this is not provided, Plotly Express will construct a dataframe using the other arguments.
- `x`: The column in the dataframe that specifies the length of the bars, plotted along the x-axis in cartesian coordinates.
- `y`: The column in the dataframe that represents the stages of the funnel, plotted along the y-axis.
- `color`: The column in the dataframe that assigns colors to the bars of the funnel.
- `facet_row`: Splits the funnel chart into vertically-stacked subplots based on a specified column from the dataframe.
- `facet_col`: Splits the funnel chart into horizontally-arranged subplots based on a specified column from the dataframe.

> **Note:** The ellipsis (...) indicates there can be additional optional parameters beyond those listed here.

## Examples

The example below generates a funnel chart representing the job search process for an applicant.

```py
import plotly.express as px
import pandas as pd

# Create sample dictionary
data = {
'Stage': ['Applications Sent', 'Phone Interview', 'Technical Interview', 'Onsite Interview', 'Offers Received', 'Offers Accepted'],
'Job Applications': [500, 348, 92, 56, 10, 1]
}

# Convert the dictionary into a DataFrame
df = pd.DataFrame(data)

# Create the funnel chart with title "Job Search"
fig = px.funnel(df, x='Job Applications', y='Stage', title='Job Search')

# Show the chart
fig.show()
```

The above example produces the following output:

![Funnel Chart Illustrating Job Search](https://raw.githubusercontent.com/Codecademy/docs/main/media/plotly-express-funnel-example1.png)

As a variation on the previous example, the next example adds subplots using the `facet_col` parameter to compare two different job applicants side by side.

```py
import plotly.express as px
import pandas as pd

# Create sample dictionary
data = {
'Stage': ['Applications Sent', 'Phone Interview', 'Technical Interview', 'Onsite Interview', 'Offers Received', 'Offers Accepted'] * 2,
'Job Applications': [500, 348, 92, 56, 10, 1, 500, 329, 290, 225, 167, 1],
'Applicants': ['Candidate 1'] * 6 + ['Candidate 2'] * 6
}

# Convert the dictionary into a DataFrame
df = pd.DataFrame(data)

# Create the funnel chart with title "Job Search Comparison"
fig = px.funnel(df, x='Job Applications', y='Stage', facet_col='Applicants', title='Job Search Comparison')

# Show the chart
fig.show()
```

The above code will result in the following output:

![Funnel Chart Comparing Two Applicants](https://raw.githubusercontent.com/Codecademy/docs/main/media/plotly-express-funnel-example2.png)
Binary file added media/plotly-express-funnel-example1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/plotly-express-funnel-example2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading