Introducing Gradio Clients

Watch

New to Gradio? Start here: Getting Started

See the Release History

BarPlot

gradio.BarPlot(ยทยทยท)
import gradio as gr import pandas as pd import numpy as np simple = pd.DataFrame(np.array( [ ["A", 28], ["B", 55], ["C", 43], ["D", 91], ["E", 81], ["F", 53], ["G", 19], ["H", 87], ["I", 52], ] ), columns=["item", "inventory"]) with gr.Blocks() as demo: gr.BarPlot( value=simple, x="item", y="inventory", title="Simple Bar Plot", container=True, width=400 ) demo.launch() pandas

Description

Creates a bar plot component to display data from a pandas DataFrame (as output). As this component does not accept user input, it is rarely used as an input component.

Behavior

As input component: (Rarely used) passes the data displayed in the bar plot as an AltairPlotData dataclass, which includes the plot information as a JSON string, as well as the type of plot (in this case, "bar").

Your function should accept one of these types:
def predict(
	value: AltairPlotData
)
	...

As output component: Expects a pandas DataFrame containing the data to display in the bar plot. The DataFrame should contain at least two columns, one for the x-axis (corresponding to this component's x argument) and one for the y-axis (corresponding to y).

Your function should return one of these types:
def predict(ยทยทยท) -> pd.DataFrame | None
	...	
	return value

Initialization

Parameters

Shortcuts

Class Interface String Shortcut Initialization

gradio.BarPlot

"barplot"

Uses default values

Demos

import gradio as gr import pandas as pd import random simple = pd.DataFrame( { "a": ["A", "B", "C", "D", "E", "F", "G", "H", "I"], "b": [28, 55, 43, 91, 81, 53, 19, 87, 52], } ) fake_barley = pd.DataFrame( { "site": [ random.choice( [ "University Farm", "Waseca", "Morris", "Crookston", "Grand Rapids", "Duluth", ] ) for _ in range(120) ], "yield": [random.randint(25, 75) for _ in range(120)], "variety": [ random.choice( [ "Manchuria", "Wisconsin No. 38", "Glabron", "No. 457", "No. 462", "No. 475", ] ) for _ in range(120) ], "year": [ random.choice( [ "1931", "1932", ] ) for _ in range(120) ], } ) def bar_plot_fn(display): if display == "simple": return gr.BarPlot( simple, x="a", y="b", title="Simple Bar Plot with made up data", tooltip=["a", "b"], y_lim=[20, 100], ) elif display == "stacked": return gr.BarPlot( fake_barley, x="variety", y="yield", color="site", title="Barley Yield Data", tooltip=["variety", "site"], ) elif display == "grouped": return gr.BarPlot( fake_barley.astype({"year": str}), x="year", y="yield", color="year", group="site", title="Barley Yield by Year and Site", group_title="", tooltip=["yield", "site", "year"], ) elif display == "simple-horizontal": return gr.BarPlot( simple, x="a", y="b", x_title="Variable A", y_title="Variable B", title="Simple Bar Plot with made up data", tooltip=["a", "b"], vertical=False, y_lim=[20, 100], ) elif display == "stacked-horizontal": return gr.BarPlot( fake_barley, x="variety", y="yield", color="site", title="Barley Yield Data", vertical=False, tooltip=["variety", "site"], ) elif display == "grouped-horizontal": return gr.BarPlot( fake_barley.astype({"year": str}), x="year", y="yield", color="year", group="site", title="Barley Yield by Year and Site", group_title="", tooltip=["yield", "site", "year"], vertical=False, ) with gr.Blocks() as bar_plot: with gr.Row(): with gr.Column(): display = gr.Dropdown( choices=[ "simple", "stacked", "grouped", "simple-horizontal", "stacked-horizontal", "grouped-horizontal", ], value="simple", label="Type of Bar Plot", ) with gr.Column(): plot = gr.BarPlot() display.change(bar_plot_fn, inputs=display, outputs=plot) bar_plot.load(fn=bar_plot_fn, inputs=display, outputs=plot) bar_plot.launch()

Event Listeners

Description

Event listeners allow you to respond to user interactions with the UI components you've defined in a Gradio Blocks app. When a user interacts with an element, such as changing a slider value or uploading an image, a function is called.

Supported Event Listeners

The BarPlot component supports the following event listeners. Each event listener takes the same parameters, which are listed in the Event Parameters table below.

Listener Description

BarPlot.change(fn, ยทยทยท)

Triggered when the value of the Plot changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See .input() for a listener that is only triggered by user input.

BarPlot.clear(fn, ยทยทยท)

This listener is triggered when the user clears the Plot using the X button for the component.

Event Parameters

Parameters