|
1 | 1 | """ pyplots.ai |
2 | 2 | bar-basic: Basic Bar Chart |
3 | | -Library: altair 6.0.0 | Python 3.13.11 |
4 | | -Quality: 93/100 | Created: 2025-12-23 |
| 3 | +Library: altair 6.0.0 | Python 3.14 |
| 4 | +Quality: 92/100 | Created: 2025-12-23 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import altair as alt |
8 | 8 | import pandas as pd |
9 | 9 |
|
10 | 10 |
|
11 | 11 | # Data - Product sales by category (realistic retail scenario) |
| 12 | +# Includes close-valued pairs (Clothing/Home & Garden, Toys/Food) to showcase comparison |
12 | 13 | data = pd.DataFrame( |
13 | 14 | { |
14 | 15 | "category": ["Electronics", "Clothing", "Home & Garden", "Sports", "Books", "Toys", "Food"], |
15 | | - "value": [45200, 32100, 28400, 21800, 18500, 15200, 12300], |
| 16 | + "value": [45200, 31500, 29800, 21800, 18500, 14200, 13100], |
16 | 17 | } |
17 | 18 | ) |
18 | 19 |
|
19 | | -# Chart |
20 | | -chart = ( |
| 20 | +# Highlight the top-performing category |
| 21 | +data["is_top"] = data["value"] == data["value"].max() |
| 22 | + |
| 23 | +# Sort order by descending value |
| 24 | +sort_order = data.sort_values("value", ascending=False)["category"].tolist() |
| 25 | + |
| 26 | +# Chart - bars with conditional color to highlight leader |
| 27 | +bars = ( |
21 | 28 | alt.Chart(data) |
22 | | - .mark_bar(color="#306998", cornerRadiusTopLeft=4, cornerRadiusTopRight=4) |
| 29 | + .mark_bar(cornerRadiusTopLeft=4, cornerRadiusTopRight=4) |
23 | 30 | .encode( |
24 | 31 | x=alt.X( |
25 | 32 | "category:N", |
26 | 33 | title="Product Category", |
27 | | - sort="-y", |
| 34 | + sort=sort_order, |
28 | 35 | axis=alt.Axis(labelAngle=-45, labelFontSize=18, titleFontSize=22), |
29 | 36 | ), |
30 | | - y=alt.Y("value:Q", title="Sales ($)", axis=alt.Axis(labelFontSize=18, titleFontSize=22, format="$,.0f")), |
| 37 | + y=alt.Y( |
| 38 | + "value:Q", |
| 39 | + title="Sales ($)", |
| 40 | + scale=alt.Scale(domain=[0, 50000]), |
| 41 | + axis=alt.Axis( |
| 42 | + labelFontSize=18, titleFontSize=22, format="$,.0f", values=[0, 10000, 20000, 30000, 40000, 50000] |
| 43 | + ), |
| 44 | + ), |
| 45 | + color=alt.condition(alt.datum.is_top, alt.value("#FFD43B"), alt.value("#306998")), |
31 | 46 | tooltip=[alt.Tooltip("category:N", title="Category"), alt.Tooltip("value:Q", title="Sales", format="$,.0f")], |
32 | 47 | ) |
33 | | - .properties(width=1500, height=800, title=alt.Title(text="bar-basic · altair · pyplots.ai", fontSize=28)) |
| 48 | +) |
| 49 | + |
| 50 | +# Value labels above bars |
| 51 | +labels = bars.mark_text(align="center", baseline="bottom", dy=-8, fontSize=16, color="#333333").encode( |
| 52 | + text=alt.Text("value:Q", format="$,.0f"), color=alt.value("#333333") |
| 53 | +) |
| 54 | + |
| 55 | +# Annotation highlighting top performer |
| 56 | +annotation = ( |
| 57 | + alt.Chart(pd.DataFrame({"category": ["Electronics"], "value": [45200], "label": ["Top seller — $45.2k"]})) |
| 58 | + .mark_text(align="center", baseline="bottom", dy=-28, fontSize=18, fontWeight="bold", color="#b8860b") |
| 59 | + .encode(x=alt.X("category:N", sort=sort_order), y=alt.Y("value:Q"), text=alt.Text("label:N")) |
| 60 | +) |
| 61 | + |
| 62 | +# Combine bars + labels + annotation |
| 63 | +chart = ( |
| 64 | + (bars + labels + annotation) |
| 65 | + .properties(width=1600, height=900, title=alt.Title(text="bar-basic · altair · pyplots.ai", fontSize=28)) |
34 | 66 | .configure_view(strokeWidth=0) |
35 | | - .configure_axis(grid=True, gridOpacity=0.3, gridDash=[4, 4]) |
| 67 | + .configure_axis(grid=False) |
| 68 | + .configure_axisY(grid=True, gridOpacity=0.15, gridDash=[4, 4]) |
36 | 69 | ) |
37 | 70 |
|
38 | | -# Save as PNG (scale_factor=3 gives 4500x2400, close to target 4800x2700) |
| 71 | +# Save as PNG (scale_factor=3 gives 4800x2700 at 1600x900) |
39 | 72 | chart.save("plot.png", scale_factor=3.0) |
40 | 73 |
|
41 | 74 | # Save interactive HTML |
|
0 commit comments