Skip to content

Changing the y label for histogram in plotly express does not work #2876

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

Closed
vikigenius opened this issue Nov 5, 2020 · 11 comments
Closed

Changing the y label for histogram in plotly express does not work #2876

vikigenius opened this issue Nov 5, 2020 · 11 comments
Milestone

Comments

@vikigenius
Copy link

Hi I am using plotly express to plot a histogram.

import plotly.express as px

px.histogram(
    cdf,
    x='diag',
    histnorm='probability',
    labels={'diag': 'cosine similarity'},
    title='Cosine Similarity of Diagonal Samples',
    color_discrete_sequence=['green'],
)

The y label is shown as count, I want the y label to be called probability.
I tried passing labels={'diag': 'cosine similarity', 'count': 'probability'} but that doesn't work? How do I change the y label ?

@set92
Copy link

set92 commented Feb 15, 2021

Keeps happening in 4.14.3

I fixed it a little bit with yaxis_title but in the popup that appear when you put the mouse over the bars still shows "count".

fig = px.histogram(
    n_orders, labels={"value": "# Pedidos", "count": "# Clientes"}
).update_layout(
    title={"text": "Numero de pedidos por cliente", "x": 0.5}, yaxis_title="# Clientes"
)
fig.show()

@mmonto95
Copy link

Keeps happening

@zhenliu2012
Copy link

Still happening with 5.6.0

@kaoutaar
Copy link

you need to change it using
fig.update_layout(yaxis_title="probability")

@nicolaskruchten
Copy link
Contributor

This works fine for me for both the y axis title and the hoverlabel... I'm not sure what the problem is.

image

@brylie
Copy link

brylie commented Apr 27, 2022

@nicolaskruchten, I am experiencing this issue today.

In our project, all texts need to be localized, so we cannot rely on auto-generated labels such as "sum of" or "probability".

The following code produces the correct chart, but the aggregate category label is prefixed with "sum of" in both the axis label and hover text.

px.histogram(
    chart_data,
    x="role_name",
    y="total_minutes",
    color="work_type",
    labels={
        # notice here the use of _("") localization function
        "role_name": _("Caregiver role"),
        "total_minutes": _("total minutes"),
        "work_type": _("Type of work"),
    },
)

image

The desired output above would be that the y-axis label and hover text would just be "total minutes" rather than "sum of total minutes", the latter of which we cannot localize.

@brylie
Copy link

brylie commented Apr 27, 2022

Given the ambiguity of the original request, I will gladly open a second issue related to changing the y-axis label and hover text, if needed.

@nicolaskruchten
Copy link
Contributor

@brylie this is a bit of a different issue but I may as well respond here so folks scrolling through find it ;)

There's no set of arguments you can pass to PX to localize these "sum of" and "probability" strings unfortunately, so you'll have to mutate them explicitly with something like the code below. For completeness, you'll need to localize not only the field names as you have done above, but also the values within the colums ("Sat", "Sun" in my example, and "Nurse" in your example above), for which there is no Plotly functionality, and you'll need to do in the underlying data.

import plotly.express as px

fig = px.histogram(px.data.tips(), x="day", y="total_bill")
fig.for_each_trace(lambda t: t.update(hovertemplate=t.hovertemplate.replace("sum of", "somme de")))
fig.for_each_yaxis(lambda a: a.update(title_text=a.title.text.replace("sum of", "somme de")))

image

@brylie
Copy link

brylie commented Apr 27, 2022

@nicolaskruchten, thank you for your quick response and examples. The example seems like a good temporary work-around while exploring the underlying issue of prepending English text to chart axis labels and hover texts.

In my case, I'd rather the text weren't prepended at all, since my axis label ("total minutes") is correct.

brylie added a commit to GeriLife/caregiving that referenced this issue Apr 27, 2022
brylie added a commit to GeriLife/caregiving that referenced this issue Apr 27, 2022
brylie added a commit to GeriLife/caregiving that referenced this issue Apr 27, 2022
brylie added a commit to GeriLife/caregiving that referenced this issue Apr 27, 2022
brylie added a commit to GeriLife/caregiving that referenced this issue Apr 27, 2022
@hrani
Copy link

hrani commented Dec 14, 2022

@nicolaskruchten, thank you for the examples, I have made small modification to the same code added animation_frame, before the play button is clicked yaxis and hovertext has no "sum of" text but once played we can see hovertext adds again sum of.
Sorry I am new to this not sure if I am doing something stupid

import plotly.express as px
import pandas as pd
import io
data = '''
time name value
0 P1 2
10 P1 4
20 P1 6
30 P1 3
0 P2 1
10 P2 4
20 P2 3
30 P2 2
'''
df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
fig = px.histogram(df, x="name", y="value",color='name',animation_frame="time",range_y=[0,df['value'].max()],color_discrete_sequence=px.colors.qualitative.T10)
fig.for_each_trace(lambda t: t.update(hovertemplate=t.hovertemplate.replace("sum of", "")))
fig.for_each_yaxis(lambda a: a.update(title_text=a.title.text.replace("sum of", "")))
fig.show()

@AlvaroAP06
Copy link

AlvaroAP06 commented Jul 25, 2023

Hi!

I've been having the same issue.

With the following code it showed the next chart:

fig = px.histogram(df, x = 'horsepower', 
                   labels = {
                       'horsepower': 'Horsepower', 
                       'count': 'Count'
                 })
fig.show()

image

And finally I've been able to fix it with the following code:

fig = px.histogram(df, x = 'horsepower')
fig.update_layout(
    xaxis_title_text = 'Horsepower', 
    yaxis_title_text = 'Count'
    )
fig.show()

image

Hope it helps!

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

9 participants