Bar#
Bar marks are useful in many visualizations, including bar charts, stacked bar charts, and timelines.
Bar Mark Properties#
Click to show code
import altair as alt
import pandas as pd
corner_slider = alt.binding_range(min=0, max=50, step=1)
corner_var = alt.param(bind=corner_slider, value=0, name="cornerRadius")
source = pd.DataFrame(
{
"a": ["A", "B", "C", "D", "E", "F", "G", "H", "I"],
"b": [28, 55, 43, 91, 81, 53, 19, 87, 52],
}
)
alt.Chart(source).mark_bar(cornerRadius=corner_var).encode(
x=alt.X("a:N").axis(labelAngle=0),
y="b:Q",
).add_params(corner_var)
A bar
mark definition can contain any standard mark properties
and the following special properties:
Click to show table
Property |
Type |
Description |
---|---|---|
width |
anyOf( |
Width of the marks. One of:
|
height |
anyOf( |
Height of the marks. One of:
|
orient |
The orientation of a non-stacked bar, tick, area, and line charts. The value is either horizontal (default) or vertical.
|
|
align |
The horizontal alignment of the text or ranged marks (area, bar, image, rect, rule). One of Note: Expression reference is not supported for range marks. |
|
baseline |
anyOf( |
For text marks, the vertical text baseline. One of For range marks, the vertical alignment of the marks. One of Note: Expression reference is not supported for range marks. |
binSpacing |
|
Offset between bars for binned field. The ideal value for this is either 0 (preferred by statisticians) or 1 (Vega-Lite default, D3 example style). Default value: |
cornerRadius |
anyOf( |
The radius in pixels of rounded rectangles or arcs’ corners. Default value: |
cornerRadiusEnd |
anyOf( |
|
cornerRadiusTopLeft |
anyOf( |
The radius in pixels of rounded rectangles’ top right corner. Default value: |
cornerRadiusTopRight |
anyOf( |
The radius in pixels of rounded rectangles’ top left corner. Default value: |
cornerRadiusBottomRight |
anyOf( |
The radius in pixels of rounded rectangles’ bottom right corner. Default value: |
cornerRadiusBottomLeft |
anyOf( |
The radius in pixels of rounded rectangles’ bottom left corner. Default value: |
Examples#
Single Bar Chart#
Mapping a quantitative field to either x
or y
of the bar
mark produces a single bar chart.
import altair as alt
from altair import datum
from vega_datasets import data
source = data.population.url
alt.Chart(source).mark_bar().encode(
alt.X("sum(people):Q").title("Population")
).transform_filter(
datum.year == 2000
)
Bar Chart#
If we map a different discrete field to the y
channel, we can produce a horizontal bar chart. Specifying alt.Step(20)
will adjust the bar’s height per discrete step.
import altair as alt
from altair import datum
from vega_datasets import data
source = data.population.url
alt.Chart(source).mark_bar().encode(
alt.X("sum(people):Q").title("Population"),
alt.Y("age:O"),
).transform_filter(
datum.year == 2000
).properties(height=alt.Step(20))
Bar Chart with a Temporal Axis#
While the bar
mark typically uses the x
and y
channels to encode
a pair of discrete and continuous fields, it can also be used with continuous
fields on both channels. For example, given a bar chart with a temporal field
on x
, we can see that the x-scale is a continuous scale. By default, the size of
bars on continuous scales will be set based on the continuousBandSize
config.
import altair as alt
from vega_datasets import data
source = data.seattle_weather()
alt.Chart(source).mark_bar().encode(
alt.X("month(date):T").title("Date"),
alt.Y("mean(precipitation):Q"),
)
Histograms#
If the data is not pre-aggregated (i.e. each record in the data field represents one item), mapping a binned quantitative field to x
and aggregate count
to y
produces a histogram.
import altair as alt
from vega_datasets import data
source = data.movies.url
alt.Chart(source).mark_bar().encode(
alt.X("IMDB_Rating:Q").bin(),
y='count()',
)
Stacked Bar Chart#
Adding color to the bar chart (by using the color
attribute) creates a stacked bar chart by default. Here we also customize the color’s scale range to make the color a little nicer. (See stack
for more details about customizing stack.)
import altair as alt
from vega_datasets import data
source = data.barley()
alt.Chart(source).mark_bar().encode(
x="variety",
y="sum(yield)",
color="site"
)
Grouped Bar Chart with Offset#
import altair as alt
import pandas as pd
source = pd.DataFrame(
{
"category": ["A", "A", "B", "B", "C", "C"],
"group": ["x", "y", "z", "x", "y", "z"],
"value": [0.1, 0.6, 0.9, 0.7, 0.2, 0.6],
}
)
alt.Chart(source).mark_bar().encode(
x=alt.X("category:N"),
xOffset="group:N",
y=alt.Y("value:Q"),
color=alt.Color("group:N"),
)