How to Add Text Labels to a Histogram in Plotly
Last Updated :
23 Jul, 2025
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:
Using text_auto in Plotly Express2. 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:
Using texttemplate in Plotly Graph ObjectsCustomizing 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:
Customizing Text Labels in HistogramAdvanced 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.