Skip to content

Commit

Permalink
[Demo] Add diverging bar to visual-vocabulary (#792)
Browse files Browse the repository at this point in the history
Co-authored-by: Li Nguyen <[email protected]>
  • Loading branch information
hxe00570 and huong-li-nguyen authored Oct 11, 2024
1 parent d21f8cb commit 3ca9193
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Highlights ✨
- A bullet item for the Highlights ✨ category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Removed
- A bullet item for the Removed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Added
- A bullet item for the Added category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Changed
- A bullet item for the Changed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Deprecated
- A bullet item for the Deprecated category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Fixed
- A bullet item for the Fixed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Security
- A bullet item for the Security category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
45 changes: 35 additions & 10 deletions vizro-core/examples/scratch_dev/app.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
"""Dev app to try things out."""

import pandas as pd
import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro._themes._color_values import COLORS

pastry = pd.DataFrame(
{
"pastry": [
"Scones",
"Bagels",
"Muffins",
"Cakes",
"Donuts",
"Cookies",
"Croissants",
"Eclairs",
"Brownies",
"Tarts",
"Macarons",
"Pies",
],
"Profit Ratio": [-0.10, -0.15, -0.05, 0.10, 0.05, 0.20, 0.15, -0.08, 0.08, -0.12, 0.02, -0.07],
}
)

df = px.data.iris()

page = vm.Page(
title="Page with asfsadfsadf",
layout=vm.Layout(grid=[[0, 1]]),
title="Diverging bar",
components=[
vm.Graph(id="scatter_chart", figure=px.scatter(df, x="sepal_length", y="petal_width", color="species")),
vm.Graph(id="hist_chart", figure=px.histogram(df, x="sepal_width", color="species")),
],
controls=[
vm.Filter(column="species", selector=vm.Dropdown(value=["ALL"])),
vm.Graph(
figure=px.bar(
pastry.sort_values("Profit Ratio"),
orientation="h",
x="Profit Ratio",
y="pastry",
color="Profit Ratio",
color_continuous_scale=COLORS["DIVERGING_RED_CYAN"],
),
),
],
)

dashboard = vm.Dashboard(pages=[page], navigation=vm.Navigation(nav_selector=vm.NavBar()))
dashboard = vm.Dashboard(pages=[page])

if __name__ == "__main__":
Vizro(serve_locally=False).build(dashboard).run()
Vizro().build(dashboard).run()
2 changes: 1 addition & 1 deletion vizro-core/examples/visual-vocabulary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ The dashboard is still in development. Below is an overview of the chart types f
| Column and line || Correlation, Time | [Multiple chart types in Plotly](https://plotly.com/python/graphing-multiple-chart-types/) | [go.Bar](https://plotly.com/python-api-reference/generated/plotly.graph_objects.Bar.html) and [go.Scatter](https://plotly.com/python-api-reference/generated/plotly.graph_objects.Scatter.html) |
| Connected scatter || Correlation, Time | [Line plot with px](https://plotly.com/python/line-charts/) | [px.line](https://plotly.com/python-api-reference/generated/plotly.express.line) |
| Cumulative curve || Distribution | | |
| Diverging bar | | Deviation | | |
| Diverging bar | | Deviation | [Bar chart with px](https://plotly.com/python/bar-charts/) | [px.bar](https://plotly.com/python-api-reference/generated/plotly.express.bar) |
| Diverging stacked bar || Deviation | | |
| Donut || Part-to-whole | [Pie chart with px](https://plotly.com/python/pie-charts/) | [px.pie](https://plotly.com/python-api-reference/generated/plotly.express.pie) |
| Dot map || Spatial | [Bubble map in px](https://plotly.com/python/bubble-maps/) | [px.scatter_map](https://plotly.github.io/plotly.py-docs/generated/plotly.express.scatter_map) |
Expand Down
1 change: 0 additions & 1 deletion vizro-core/examples/visual-vocabulary/chart_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class ChartGroup:
name="Deviation",
pages=pages.deviation.pages,
incomplete_pages=[
IncompletePage(title="Diverging bar"),
IncompletePage("Diverging stacked bar"),
IncompletePage(title="Surplus deficit filled line"),
],
Expand Down
21 changes: 21 additions & 0 deletions vizro-core/examples/visual-vocabulary/pages/_pages_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,24 @@ def make_code_clipboard_from_py_file(filepath: str):
"measure": ["relative", "relative", "total", "relative", "relative", "total"],
}
)


pastries = pd.DataFrame(
{
"pastry": [
"Scones",
"Bagels",
"Muffins",
"Cakes",
"Donuts",
"Cookies",
"Croissants",
"Eclairs",
"Brownies",
"Tarts",
"Macarons",
"Pies",
],
"Profit Ratio": [-0.10, -0.15, -0.05, 0.10, 0.05, 0.20, 0.15, -0.08, 0.08, -0.12, 0.02, -0.07],
}
)
49 changes: 48 additions & 1 deletion vizro-core/examples/visual-vocabulary/pages/deviation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
"""Deviation charts."""

import plotly.io as pio
import vizro.models as vm
import vizro.plotly.express as px

from pages._factories import butterfly_factory
from pages._pages_utils import PAGE_GRID, make_code_clipboard_from_py_file, pastries

butterfly = butterfly_factory("deviation")

pages = [butterfly]

diverging_bar = vm.Page(
title="Diverging bar",
path="deviation/diverging-bar",
layout=vm.Layout(grid=PAGE_GRID),
components=[
vm.Card(
text="""
#### What is a diverging bar?
A diverging bar chart is a version of a bar chart used to display both positive and negative values across
a common baseline. Bars extend either to the left or right of the central axis, indicating negative or
positive values, respectively. This allows for easy comparison of data points that deviate in opposite
directions.
&nbsp;
#### When should I use it?
Use a diverging bar chart to compare positive and negative values from a central baseline. These charts are
suitable for visualizing profit and loss, survey results, and performance metrics. Apply color coding
effectively, using distinct colors for positive and negative values to quickly distinguish categories.
Alternatively, use a continuous diverging color scale for a more nuanced view, especially with a wide range
of values. Ensure a consistent scale on both sides of the baseline to avoid misleading interpretations.
"""
),
vm.Graph(
figure=px.bar(
pastries.sort_values("Profit Ratio"),
orientation="h",
x="Profit Ratio",
y="pastry",
color="Profit Ratio",
color_continuous_scale=pio.templates["vizro_dark"].layout.colorscale.diverging,
color_continuous_midpoint=0,
),
),
make_code_clipboard_from_py_file("diverging_bar.py"),
],
)

pages = [butterfly, diverging_bar]
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pandas as pd
import plotly.io as pio
import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro

pastries = pd.DataFrame(
{
"pastry": [
"Scones",
"Bagels",
"Muffins",
"Cakes",
"Donuts",
"Cookies",
"Croissants",
"Eclairs",
"Brownies",
"Tarts",
"Macarons",
"Pies",
],
"Profit Ratio": [-0.10, -0.15, -0.05, 0.10, 0.05, 0.20, 0.15, -0.08, 0.08, -0.12, 0.02, -0.07],
}
)

page = vm.Page(
title="Diverging bar",
components=[
vm.Graph(
figure=px.bar(
pastries.sort_values("Profit Ratio"),
orientation="h",
x="Profit Ratio",
y="pastry",
color="Profit Ratio",
color_continuous_scale=pio.templates["vizro_dark"].layout.colorscale.diverging,
color_continuous_midpoint=0,
),
),
],
)

dashboard = vm.Dashboard(pages=[page])
Vizro().build(dashboard).run()

0 comments on commit 3ca9193

Please sign in to comment.