Skip to content

Commit 0ddcc6e

Browse files
feat(altair): implement bar-basic
1 parent 4cecc77 commit 0ddcc6e

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
bar-basic: Basic Bar Chart
3+
Library: altair
4+
"""
5+
6+
import altair as alt
7+
import pandas as pd
8+
9+
10+
# Data
11+
data = pd.DataFrame(
12+
{
13+
"category": ["Electronics", "Clothing", "Home & Garden", "Sports", "Books", "Toys", "Food"],
14+
"value": [45200, 32100, 28400, 21800, 18500, 15200, 12300],
15+
}
16+
)
17+
18+
# Chart
19+
chart = (
20+
alt.Chart(data)
21+
.mark_bar(color="#306998", cornerRadiusTopLeft=3, cornerRadiusTopRight=3)
22+
.encode(
23+
x=alt.X(
24+
"category:N",
25+
title="Product Category",
26+
sort="-y",
27+
axis=alt.Axis(labelAngle=-45, labelFontSize=16, titleFontSize=20),
28+
),
29+
y=alt.Y("value:Q", title="Sales ($)", axis=alt.Axis(labelFontSize=16, titleFontSize=20)),
30+
tooltip=[alt.Tooltip("category:N", title="Category"), alt.Tooltip("value:Q", title="Sales", format="$,.0f")],
31+
)
32+
.properties(width=1600, height=900, title=alt.Title(text="Product Sales by Category", fontSize=24))
33+
.configure_view(strokeWidth=0)
34+
.configure_axis(grid=True, gridOpacity=0.3)
35+
)
36+
37+
# Save as PNG and HTML
38+
chart.save("plot.png", scale_factor=3.0)
39+
chart.save("plot.html")

0 commit comments

Comments
 (0)