Display hover data based on the original order of hover_data variable
I have a dataframe like this (see privatebin for full csv, expire in 30 days):
Now, I want to display a bar chart with hover data ordered like this (see the labels from code block below):
- Job Position
- State
- Job Count (Per State)
- Job Count (Whole Country)
- Avg Number of Applicants (Per State)
- Avg Number of Applicants (Whole Country)
- Avg Number of Views (Per State)
- Avg Number of Views (Whole Country)
So I make the code like this (notice that the hover_data order is the same as above list):
df = pd.read_csv('data.csv')
fig = px.bar(
data_frame = df,
x = 'title',
y = f'avg_applies',
title = 'Number of Applicants for Various Job Positions',
color = 'state',
color_discrete_sequence = px.colors.qualitative.Pastel,
labels = {
'title': 'Job Position',
'state': 'State',
'count': 'Job Count (Per State)',
'sum_count': 'Job Count (Whole Country)',
'applies': 'Avg Number of Applicants (Per State)',
f'avg_applies': 'Avg Number of Applicants (Whole Country)',
'views': 'Avg Number of Views (Per State)',
f'avg_views': 'Avg Number of Views (Whole Country)',
},
# Extra data to show
hover_data = {
'title': True, # x
'state': True, # color
'count': ':.0f',
'sum_count': ':.0f',
'applies': ':.0f',
f'avg_applies': ':.0f', # y
'views': ':.0f',
f'avg_views': ':.0f'
}
)
The problem is, because x, y, and color each are already set as title (job position), avg_applies (avg number of applicants), and state, the order will become something like this (which is not what I want):
- State ->
color - Job Position ->
x - Avg Number of Applicants (Whole Country) ->
y - Job Count (Per State)
- Job Count (Whole Country)
- Avg Number of Applicants (Per State)
- Avg Number of Views (Per State)
- Avg Number of Views (Whole Country)
It would be nice to be able to display hover data based on the original order of hover_data variable. Which, in my case is ordered like this:
hover_data = {
'title': True, # x
'state': True, # color
'count': ':.0f',
'sum_count': ':.0f',
'applies': ':.0f',
f'avg_applies': ':.0f', # y
'views': ':.0f',
f'avg_views': ':.0f'
}
Ugly Workarounds:
I know that it's possible to modify the order by using hovertemplate, but I still need to define the labels parameter (because without labels, x and y axis title and the legend will be written as raw variable name). For me, writing both hovertemplate and labels are like writing the same thing twice (and make the code unnecessarily longer).
I also know that it's possible to rename the column directly in Pandas (so the labels parameter is not needed anymore), but it's still twice the work when using hovertemplate and not really possible when working with SQL. Using Spark or DuckDB and converting it to Pandas first is not efficient and will result an extra overhead.
What's currently being displayed:
What I want (edited using Paint):
It's a feature request, unless I don't know if there's a more elegant workaround.
Hi - we are tidying up stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for a while, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. If you'd like to submit a PR, we'd be happy to prioritize a review, and if it's a request for tech support, please post in our community forum. Thank you - @gvwilson