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

adding a diverging bar example to the horizontal bar documentation #4994

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
74 changes: 73 additions & 1 deletion doc/python/horizontal-bar-charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,78 @@ fig.update_layout(annotations=annotations)
fig.show()
```

### Diverging Bar (or Butterfly) Chart

Diverging bar charts show counts of positive outcomes or sentiments to the right of zero and counts of negative outcomes to the left of zero, allowing the reader to easily spot areas of excellence and concern. Implementing presentation-ready versions of them in Plotly requires a few non standard layout and legendrank options.

```
import pandas as pd
import plotly.graph_objects as go

data = {
"Category": ["Content Quality", "Instructor Effectiveness", "Ease of Use", "Customer Support", "Value for Money"],
"Somewhat Agree": [30, 25, 40, 20, 49],
"Strongly Agree": [40, 35, 50, 30, 60],
"Somewhat Disagree": [-20, -15, -25, -10, -30],
"Strongly Disagree": [-10, -50, -15, -15,-20]
}
df = pd.DataFrame(data)
print(df.columns)

fig = go.Figure()

color_by_category={
"Strongly Agree":'darkblue',
"Somewhat Agree":'lightblue',
"Somewhat Disagree":'orange',
"Strongly Disagree":'red',
}

# We want the legend to be ordered in the same order that the categories appear, left to right --
# which is different from the order in which we add the traces to the figure.
# since we need to create the "somewhat" traces first, then the "strongly" traces to display
# the segments in the desired order

legend_rank_by_category={
"Strongly Disagree":1,
"Somewhat Disagree":2,
"Somewhat Agree":3,
"Strongly Agree":4,
}

# Add bars for each category
for col in df.columns[1:]:
fig.add_trace(go.Bar(
y=df["Category"],
x=df[col],
name=col,
orientation='h',
marker=dict(color=color_by_category[col]),
legendrank=legend_rank_by_category[col]

))


fig.update_layout(
title="Reactions to the statement, 'The service met your expectations for':",
xaxis=dict(
title="Number of Responses",
zeroline=True, # Ensure there's a zero line for divergence
zerolinecolor="black",
# use array tick mode to show that the counts to the left of zero are still positive.
# this is hard coded; generalize this if you plan to create a function that takes unknown or widely varying data
tickmode = 'array',
tickvals = [-50, 0, 50, 100],
ticktext = [50, 0, 50, 100]
),
yaxis_title = "",
barmode='relative', # Allows bars to diverge from the center
plot_bgcolor="white",
)

fig.show()
```

### Bar Chart with Line Plot

```python
Expand Down Expand Up @@ -335,4 +407,4 @@ fig.show()

### Reference

See more examples of bar charts and styling options [here](https://plotly.com/python/bar-charts/).<br> See https://plotly.com/python/reference/bar/ for more information and chart attribute options!
See more examples of bar charts and styling options [here](https://plotly.com/python/bar-charts/).<br> See https://plotly.com/python/reference/bar/ for more information and chart attribute options!
4 changes: 4 additions & 0 deletions doc/python/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ The following [array data types](https://numpy.org/devdocs/reference/arrays.scal

Arrays or data types that are not supported for base64 encoding to Plotly.js's typed arrays specification will still work and render correctly with Plotly. Those arrays and or data types just won't have the performance benefits that Plotly.js's base64 typed arrays feature provides.

### Dash Design Kit Compatibility

The Dash Design Kit `Graph` component from versions of Dash Design Kit earlier than 1.13 doesn't work with Plotly.py version 6 when using Python objects listed in the previous "Arrays and Data Types Supported". Upgrade Dash Design Kit to 1.13 or later to resolve this issue.

### Unsupported Attributes

Arrays passed to attributes with the following names are not supported for base64 encoding for rendering with Plotly.js.
Expand Down
5 changes: 4 additions & 1 deletion doc/python/v6-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ pip install anywidget

Plotly.py now takes advantage of recent changes in how Plotly.js handles typed arrays for improved performance. See the [performance page](https://plotly.com/python/performance/) for more details.

> If you are using Plotly.py 6 or later with Dash Design Kit, you may need to upgrade your Dash Design Kit version. See the [Dash Design Kit Compatibility section on the performance page](/python/performance/#dash-design-kit-compatibility) for more details.


## Dataframe Support

Plotly Express now uses [Narwhals](https://narwhals-dev.github.io/narwhals/) to natively support pandas, Polars, and PyArrow. With this change, the [performance](https://plotly.com/python/performance/) of using Polars or PyArrow with Plotly Express is significantly improved.
Plotly Express now uses [Narwhals](https://narwhals-dev.github.io/narwhals/) to natively support pandas, Polars, and PyArrow. With this change, the [performance](https://plotly.com/python/performance/) of using Polars or PyArrow with Plotly Express is significantly improved.

## Mapbox Deprecation

Expand Down