-
Notifications
You must be signed in to change notification settings - Fork 142
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
[Demo] Add lollipop chart to ViViVo #874
base: main
Are you sure you want to change the base?
Changes from 13 commits
f28ca8c
bb0537a
b90d126
7d2f165
644a9c7
fa2f8eb
04c6790
143a4af
3cd607d
d2e5190
7f447da
8545946
c58aeb1
eebd889
c5994db
1980b53
4bcc3ea
6ca75dc
5dda70d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -310,3 +310,45 @@ def diverging_stacked_bar(data_frame: pd.DataFrame, **kwargs) -> go.Figure: | |
fig.add_hline(y=0, line_width=2, line_color="grey") | ||
|
||
return fig | ||
|
||
|
||
@capture("graph") | ||
def lollipop(data_frame: pd.DataFrame, **kwargs): | ||
"""Creates a lollipop based on px.scatter. | ||
|
||
A lollipop chart is a variation of a bar chart where each data point is represented by a line and a dot at the end | ||
to mark the value. | ||
|
||
Inspired by: https://community.plotly.com/t/how-to-make-dumbbell-plots-in-plotly-python/47762 | ||
huong-li-nguyen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Args: | ||
data_frame: DataFrame for the chart. Can be long form or wide form. | ||
See https://plotly.com/python/wide-form/. | ||
**kwargs: Keyword arguments to pass into px.scatter (e.g. x, y, labels). | ||
See https://plotly.com/python-api-reference/generated/plotly.scatter.html. | ||
|
||
Returns: | ||
go.Figure: Lollipop chart. | ||
""" | ||
# Unlike the column_and_line chart, where all traces hold equal significance, here the traces differ in importance. | ||
# The primary scatter plot is the main trace, while the additional traces merely serve as connecting lines. | ||
# Therefore, should we apply the kwargs solely to the main scatter plot, as illustrated below? | ||
fig = px.scatter(data_frame, **kwargs) | ||
Comment on lines
+333
to
+336
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One question: In this case, there is one main scatter trace and several subsidiary scatter traces that only add lines. I assume in this case, we only add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think this is totally correct 👍. |
||
|
||
# Enable for both orientations | ||
orientation = fig.data[0].orientation | ||
x_array = fig.data[0]["x"] | ||
y_array = fig.data[0]["y"] | ||
|
||
for i in range(len(data_frame)): | ||
fig.add_trace( | ||
go.Scatter( | ||
x=[0, x_array[i]] if orientation == "h" else [x_array[i], x_array[i]], | ||
y=[y_array[i], y_array[i]] if orientation == "h" else [0, y_array[i]], | ||
mode="lines", | ||
) | ||
) | ||
|
||
fig.update_traces(marker_size=12, line_width=3, line_color=fig.layout.template.layout.colorway[0]) | ||
fig.update_layout(showlegend=False, yaxis_title="", yaxis_showgrid=False) | ||
return fig |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pandas as pd | ||
import plotly.express as px | ||
import plotly.graph_objects as go | ||
from vizro.models.types import capture | ||
|
||
gapminder = px.data.gapminder() | ||
|
||
|
||
@capture("graph") | ||
def lollipop(data_frame: pd.DataFrame, **kwargs): | ||
"""Creates a lollipop chart using Plotly.""" | ||
fig = px.scatter(data_frame, **kwargs) | ||
|
||
orientation = fig.data[0].orientation | ||
x_array = fig.data[0]["x"] | ||
y_array = fig.data[0]["y"] | ||
|
||
for i in range(len(data_frame)): | ||
fig.add_trace( | ||
go.Scatter( | ||
x=[0, x_array[i]] if orientation == "h" else [x_array[i], x_array[i]], | ||
y=[y_array[i], y_array[i]] if orientation == "h" else [0, y_array[i]], | ||
mode="lines", | ||
) | ||
) | ||
|
||
fig.update_traces(marker_size=12, line_width=3, line_color=fig.layout.template.layout.colorway[0]) | ||
fig.update_layout(showlegend=False, yaxis_title="", yaxis_showgrid=False) | ||
return fig | ||
|
||
|
||
fig = lollipop( | ||
data_frame=gapminder.query("year == 2007 and gdpPercap > 36000").sort_values("gdpPercap"), | ||
y="country", | ||
x="gdpPercap", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old code for reference
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new code is much better 🥇