HOWTO · Plotly
How to Plot Bar Chart in Plotly
Create and customize a bar chart with Plotly Express in Python.
On this page
Plotly Express creates interactive bar charts in Python. Use px.bar to map category labels and values to a chart, then adjust its orientation, colors, labels, and traces to suit the data. Install Plotly in the Python environment before running the example.
Create a basic bar chart with Plotly Express
Pass category names to x and their numeric values to y. Plotly Express creates one bar for each row or pair of corresponding values. Call fig.show() in an interactive environment to display the figure; the runnable example prints a short summary instead, so it also works where a browser display is unavailable.
Run this example:
import plotly.express as px
labels = ["One", "Two", "Three"]
values = [10, 50, 100]
fig = px.bar(x=labels, y=values, height=400, width=500)
print(f"Categories: {len(labels)}; values: {values}")
Categories: 3; values: [10, 50, 100]
The figure dimensions are in pixels. This chart has three categories with heights 10, 50, and 100. The rendered result should show those labels and values; the image below is the supplied basic-chart asset.
Rendered result: the three category labels and their corresponding bar heights.
When working from a data frame, pass its column names, for example px.bar(data_frame, x="category", y="value"). This keeps the mapping explicit and lets Plotly use each row as an observation.
Choose the bar orientation and configure axes
By default, px.bar draws vertical bars: the x values identify categories and the y values set their heights. Set orientation="h" to draw horizontal bars. For horizontal bars, map the numeric measurements to x and the category labels to y; Plotly determines the bar direction from the orientation setting, so there is no need to swap columns blindly.
For example, a horizontal chart can be created with px.bar(x=values, y=labels, orientation="h"). Use fig.update_layout(title="Sales by category") to set a figure title. In the default vertical chart, y carries the numeric values, so use fig.update_yaxes(title_text="Sales", type="log") when a logarithmic numeric axis is appropriate. For a horizontal chart with numeric values on x, configure that axis with fig.update_xaxes(...). A log axis cannot display nonpositive values, so use it only when the data and comparison call for it. height and width control the overall figure size, not individual bars. To display the chart, call fig.show() in an environment with a Plotly renderer configured.
Customize bar colors
Use color with a categorical field when each category should have its own color. color_discrete_map assigns specific colors to category values; categories omitted from the mapping use Plotly’s color sequence rather than a random color. For example, pass color=labels and color_discrete_map={"One": "green", "Two": "cyan", "Three": "yellow"} to assign the three labels explicitly.
For a numeric field, use it as color to encode magnitude with a continuous color scale. For example, px.bar(x=labels, y=values, color=values, color_continuous_scale=px.colors.sequential.Rainbow) assigns colors according to the numeric values. A continuous scale represents an ordered range; a discrete map represents named categories. See the Plotly built-in color scales reference for available scales and sequences.
Add labels and style bars
Pass a sequence to text to display values or other labels on bars. Adjust placement with textposition, such as "outside" or "inside"; placement can depend on available space. For styling after chart creation, fig.update_traces applies options to the bar trace. For example, create a chart with text=["First", "Second", "Third"], then call fig.update_traces(textposition="outside", textfont_size=16, marker=dict(line=dict(color="green", width=3))) to add labels and a green outline.
fig.update_traces accepts trace properties such as text placement, marker line, opacity, and error bars. In px.bar, error_x and error_y describe uncertainty along the corresponding data axis, while base sets where each bar begins. Use these only when they accurately represent the data. Consult the Plotly bar chart reference for the full set of trace properties.