Franz Diebold
Franz Diebold

Reputation: 640

Plotly: How to manually specify the label of an aggregated field in plotly.py

In Plotly.py histogram and Density heatmaps an aggregation function (histfunc) such as sum or avg may be specified. The x and (unaggregated) y axis labels can be manually specified via the labels dict, but what about the label of the aggregated dimension?

How can the label of the aggregated dimension be manually specified?

Example histogram for code below

import plotly.express as px

df = px.data.tips()
fig = px.histogram(
    df,
    x="total_bill",
    y="tip",
    histfunc="avg",
    labels={"total_bill": "Total bill", "tip": "Tip", "??": "Average tip"},
)
fig.show()

Update/Added

And what about the z axis (?) label for a density heatmap?

Density heatmap

import plotly.express as px

df = px.data.iris()

fig = px.density_heatmap(
    df, x="petal_length", y="petal_width", z="sepal_length", histfunc="avg"
)
fig.show()

Upvotes: 0

Views: 3824

Answers (2)

r-beginners
r-beginners

Reputation: 35230

The title of the color bar can be set as follows.

import plotly.express as px

df = px.data.iris()

fig = px.density_heatmap(
    df, x="petal_length", y="petal_width", z="sepal_length", histfunc="avg"
)
fig.layout['coloraxis']['colorbar']['title'] = 'new colorbar_title'
fig.show()

enter image description here

Upvotes: 1

Prats
Prats

Reputation: 634

You can use the function update_layout to set y axis label. Example:

px.histogram(
    df,
    x="total_bill",
    y="tip",
    histfunc="avg",
    labels={"total_bill": "Total bill", "tip": "Tip", "??": "Average tip"},
).update_layout(yaxis_title="label for Y axis")

Upvotes: 2

Related Questions