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

[BUG] px.histogram does not show average when histfunc=avg #3112

Closed
ua-chjb opened this issue Dec 28, 2024 · 3 comments
Closed

[BUG] px.histogram does not show average when histfunc=avg #3112

ua-chjb opened this issue Dec 28, 2024 · 3 comments

Comments

@ua-chjb
Copy link

ua-chjb commented Dec 28, 2024

With environment as follow:

Name: plotly
Version: 5.24.1
Summary: An open-source, interactive data visualization library for Python
Home-page: https://plotly.com/python/
Author: Chris P
Author-email: [email protected]
License: MIT
Location: c:\users\benno\appdata\local\programs\python\python39\lib\site-package
s
Requires: packaging, tenacity
Required-by: dash
---
Name: dash
Version: 2.18.2
Summary: A Python framework for building reactive web-apps. Developed by Plotly.
Home-page: https://plotly.com/dash
Author: Chris Parmer
Author-email: [email protected]
License: MIT
Location: c:\users\benno\appdata\local\programs\python\python39\lib\site-package
s
Requires: dash-core-components, dash-html-components, dash-table, Flask, importl
ib-metadata, nest-asyncio, plotly, requests, retrying, setuptools, typing-extens
ions, Werkzeug
Required-by: dash-bootstrap-components, dash_mantine_components
---
Name: dash-core-components
Version: 2.0.0
Summary: Core component suite for Dash
Home-page: UNKNOWN
Author: Chris Parmer <[email protected]>
Author-email: [email protected]
License: MIT
Location: c:\users\benno\appdata\local\programs\python\python39\lib\site-package
s
Requires:
Required-by: dash
---
Name: dash_mantine_components
Version: 0.15.0
Summary: Plotly Dash Components based on Mantine
Home-page: https://github.com/snehilvj/dash-mantine-components
Author: Snehil Vijay <[email protected]>
Author-email: [email protected]
License: MIT
Location: c:\users\benno\appdata\local\programs\python\python39\lib\site-package
s
Requires: dash
Required-by:


Describe the bug

I create a group of histograms with px.histogram() with facet parameter set. I want to display the average so set the histfunc parameter to avg. Specific line copied here.

px.histogram( ... histfunc="avg" ... )

Although I clearly specify the avg parameter, the resulting graph shows count. This can be seen both in the initial graph (below) and with the fact that when histfunc is changed to count, the graph refreshes with identical content.

Image

Full MRE:

import pandas as pd
import numpy as np

repo = "s3://mre-examples/_ab_cleaned.csv"
_ab_ = pd.read_csv(repo)

import plotly.express as px
from plotly.offline import *

metrics_lst = ["converted", "user_journey_flag", "time_on_site", "bounce_rate", "pages_per_sesion"]
colors_lst = ["lightblue", "lightgreen"]

def histogram_facets(metric, nbins=2):
    return px.histogram(
        _ab_.sort_values(by="group", ascending=False), x=metric, color="group", nbins=nbins, facet_col="hour_segment", facet_col_wrap=3,
        category_orders={"hour_segment": ["Middle of night", "Morning", "Lunch", "Afternoon", "Evening", "Night"] },
        barmode="group", 
        histfunc="avg",  # this is the finnicky param
        color_discrete_sequence=colors_lst,
        facet_row_spacing=0.2, facet_col_spacing=0.08
    )

histogram_facets(metrics_lst[0])

let me know if this is user error (first time submitting a bug) or if the above code replicates the issue in other environment as well.

@ua-chjb ua-chjb changed the title [BUG] [BUG] px.histogram does not show average when histfunc=avg Dec 28, 2024
@AnnMarieW
Copy link
Collaborator

Hi @ua-chjb
With the histfunc you also need to specify a y column. Note that the y axis in the image is labeled as avg of None

@ua-chjb
Copy link
Author

ua-chjb commented Dec 31, 2024

Ah got it, so it is expected behavior. Noted, thanks!

Out of curiosity, why is that? It was my understanding that histograms typically plot just one variable.

@AnnMarieW
Copy link
Collaborator

You can find more info here: https://plotly.com/python/histograms/#aggregating-with-other-functions-than-count

@AnnMarieW AnnMarieW reopened this Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants