Open In App

How to Add Text Labels to a Histogram in Plotly

Last Updated : 12 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Plotly is a powerful and versatile library for creating interactive visualizations in Python. Among its many features, Plotly allows users to create histograms, which are essential for visualizing the distribution of numerical data. Adding text labels to histograms can enhance the interpretability of the data by displaying the count or percentage directly on the bars. This article will guide you through the process of adding text labels to histograms in Plotly, using both Plotly Express and Plotly Graph Objects.

Plotly Express vs. Plotly Graph Objects

Plotly provides two main interfaces for creating visualizations: Plotly Express and Plotly Graph Objects.

  • Plotly Express is a high-level interface that simplifies the creation of common visualizations with minimal code. It is ideal for quick and simple plots.
  • Plotly Graph Objects is a lower-level interface that offers more customization and control over the visualization elements.

Adding Text Labels to Histograms in Plotly

Adding text labels to histogram bars can enhance the readability and usefulness of the plot. Plotly provides several ways to achieve this.

1. Using text_auto in Plotly Express

Plotly Express introduced the text_auto parameter in version 5.5, which simplifies the process of adding text labels to histogram bars. This parameter automatically adds text labels to the bars, displaying the count or percentage of data points in each bin.

Python
import plotly.express as px
data = px.data.tips()

# Create a histogram with text labels
fig = px.histogram(data, x="total_bill", text_auto=True)
fig.show()

Output:

Screenshot-69
Using text_auto in Plotly Express

2. Using texttemplate in Plotly Graph Objects

For more control over the text labels, Plotly Graph Objects can be used with the texttemplate argument.

Python
import plotly.graph_objects as go

data = px.data.tips()

# Create a histogram with text labels
fig = go.Figure(data=[go.Histogram(x=data["total_bill"], texttemplate="%{y}")])
fig.update_layout(title=dict(text="Distribution of Total Bill"))
fig.show()

Output:

Screenshot-70
Using texttemplate in Plotly Graph Objects

Customizing Text Labels in Histogram

Text labels can be customized further by adjusting their size, color, and position.

Python
import plotly.graph_objects as go

data = px.data.tips()

# Create a histogram with customized text labels
fig = go.Figure(data=[go.Histogram(x=data["total_bill"], texttemplate="%{y}", textfont_size=20)])
fig.update_layout(title=dict(text="Distribution of Total Bill"))
fig.show()

Output:

Screenshot-73
Customizing Text Labels in Histogram

Advanced Customization

For more advanced customization, you can use additional parameters and methods provided by Plotly:

  • Text Position: Use textposition to specify where the text should appear relative to the bars (e.g., 'inside', 'outside', 'auto').
  • Text Formatting: Use texttemplate to format text labels using placeholders (e.g., %{y} for y-values).
  • Text Font: Customize the font using textfont to set the font family, size, and color.

Conclusion

Adding text labels to histograms in Plotly enhances the readability and interpretability of the data. Whether you use Plotly Express for quick and straightforward plots or Plotly Graph Objects for more detailed customization, Plotly provides powerful tools to create informative visualizations. By following the examples and techniques outlined in this article, you can effectively add and customize text labels in your histograms, making your data visualizations more insightful and engaging.


Next Article

Similar Reads