How to create a histogram with Plotly Express in Python

Plotly Express is a Python library designed for creating interactive and customizable data visualizations, including histograms.

Histograms illustrate the distribution of a continuous numerical variable by dividing the data into bins and displaying the frequency or count of values within each bin.

Features of histograms

Some of the key features of histograms include:

Simplified syntax: Plotly Express provides a high-level interface that simplifies the histogram creation process. We can generate a histogram plot with just a few lines of code.

Automatic binning: Plotly Express automatically determines the appropriate bins based on the data range and distribution. This helps in creating a balanced representation of the data.

Customizable binning: If desired, manually specify the number of bins using the nbins parameter. This gives us control over the granularity of the histogram.

Aggregation functions: Plotly Express allows us to apply aggregation functions to the data within each bin. We can compute statistics such as the sum, average, or standard deviation of the values within each bin, providing additional insights.

Grouping and coloring: We can group the data by a categorical variable. This enables us to visualize the distribution of the numerical variable across different groups.

Marginal distributions: Plotly Express allows displaying marginal distributions alongside the histogram. We can add a rug plot or a box plot to show additional information about the data distribution.

Interactive plots: The histogram plots created with Plotly Express are interactive by default. We can zoom in/out, pan, and hover over the bins to view detailed information. This interactivity enhances data exploration and analysis.

Styling and customization: Plotly Express offers a wide range of customization options for histograms. We can modify the plot title, axis labels, colors, font styles, and more to match our desired visual aesthetics.

Export and sharing: Once we've created a histogram plot, we can easily save it as an HTML file or as an image file (e.g., PNG, SVG). This allows us to share the visualizations with others or embed them in web pages or reports.

Syntax

The histogram() function syntax typically follows this structure:

import plotly.express as px
fig = px.histogram(df, x='column_name')
Syntax of the histogram() function

Parameters

The histogram() function of Plotly Express takes several parameters that allow for customization and fine-tuning of the resulting histogram plot. These parameters include:

  • data_frame: The DataFrame or data source containing the data to be plotted.

  • x: Specifies the column name or variable in the DataFrame that will be plotted on the x-axis of the histogram.

  • nbins: The number of bins or intervals to divide the data into. This controls the granularity of the histogram.

  • color: Allows us to group or color the histogram bars based on a categorical variable in the DataFrame.

  • title: Sets the title of the histogram.

  • plot.labels: A dictionary specifying the labels for the x and y axes. For example, labels={‘x’: ‘X-axis label’, ‘y’: ‘Y-axis label’}.

  • opacity: Sets the opacity of the histogram bars, ranging from 0 (completely transparent) to 1 (completely opaque).

  • histnorm: Specifies the normalization mode for the histogram. Options include 'percent' (percentage), 'probability' (normalized to a probability density), 'density' (normalized to a density), or 'probability density' (equivalent to ‘density’).

  • marginal: Adds marginal distributions to the histogram. Options include 'rug' (rug plot), 'box' (box plot), 'violin' (violin plot), or 'box+violin' (both box and violin plots).

  • barmode: Specifies the mode of the histogram bars. Options include 'overlay' (bars are overlaid), 'group' (bars are grouped side by side), or 'stack' (bars are stacked on top of each other).

  • hover_data: Additional columns or variables from the DataFrame to include in the hover tooltip when hovering over the histogram bars.

  • range_x: Specifies the range of values to be displayed on the x-axis.

  • log_x: Sets the x-axis to a logarithmic scale. Options are 'True', 'False', or 'auto'.

  • template: Specifies the plotly.js template to be used for the plot. Options include 'plotly', 'plotly_white', 'plotly_dark', 'ggplot2', 'seaborn', and more.

Return type

The px.histogram() function returns a Plotly figure object that can be displayed with fig.show(). The figure object contains all the information required. It encapsulates the data, layout, and other attributes of the histogram.

Implementation

In the following playground, we create a histogram plot using a sample dataset called tips provided by Plotly Express. Used attributes (total_bill) defined as follows:

  • total_bill: This attribute represents the total bill amount for a given meal, including the cost of food, drinks, taxes, and any additional charges. It is a continuous numeric variable representing monetary values.

# Import necessary libraries
import plotly.express as px
import pandas as pd

# Load the dataset provided by Plotly Express
df = px.data.tips()

# Display the first five rows of the data
print(df.head())

# Create a histogram using Plotly Express
fig = px.histogram(df, x="total_bill", nbins=20)

# Show the histogram
fig.show()
Creating a histogram of tips dataset

Explanation

The code above is explained in detail below:

  • Lines 2–3: import the required libraries for the code: plotly.express as px for creating the density heatmap plot, and pandas as pd for handling data in a DataFrame.

  • Line 6: Loads a sample dataset called tips using the px.data.tips() function provided by Plotly Express. The dataset contains information about restaurant tips.

  • Line 9: Prints the first five rows of the loaded dataset. The head() function retrieves the top rows of the DataFrame and print() displays the result in the console. It helps to inspect the data and verify its structure quickly.

  • Line 12: The px.histogram() function is used to generate the histogram plot. We pass the df DataFrame (which contains the loaded dataset) as the first argument. We specify the column to be plotted on the x-axis using the x parameter, which is set to total_bill. The nbins parameter is used to set the number of bins for the histogram.

  • Line 22: Display the plot using the fig.show() method, which shows the interactive plot.

Conclusion

Plotly Express provides a powerful and intuitive way to create interactive histograms in Python. Histograms are useful for visualizing the distribution of a continuous numerical variable by dividing the data into bins and displaying the frequency or count of values within each bin. With Plotly Express, we can easily create histograms using pandas DataFrames. We can also customize various parameters such as the number of bins, opacity, and bar mode, and adjust the layout for better visualization. The interactive nature of Plotly Express histograms allows for exploration and interaction with the data, enabling users to gain valuable insights and understand the underlying patterns and distributions within their datasets.

Unlock your potential: Plotly Graphing and Visualization series, all in one place!

To deepen your understanding of data visualization using Plotly, explore our comprehensive Answer series below:

Plotly express: quick and intuitive visualization

Plotly Graph Objects: Customization and advanced features

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved