|
1 | 1 | """ pyplots.ai |
2 | 2 | bar-basic: Basic Bar Chart |
3 | | -Library: bokeh 3.8.1 | Python 3.13.11 |
4 | | -Quality: 92/100 | Created: 2025-12-23 |
| 3 | +Library: bokeh 3.8.2 | Python 3.14 |
| 4 | +Quality: 90/100 | Created: 2025-12-23 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | from bokeh.io import export_png |
8 | | -from bokeh.models import ColumnDataSource, LabelSet |
| 8 | +from bokeh.models import ColumnDataSource, LabelSet, NumeralTickFormatter |
9 | 9 | from bokeh.plotting import figure, output_file, save |
10 | 10 |
|
11 | 11 |
|
12 | | -# Data - Product sales by category |
13 | | -categories = ["Electronics", "Clothing", "Home & Garden", "Sports", "Books", "Toys"] |
14 | | -values = [42500, 31200, 28700, 19800, 15400, 12600] |
| 12 | +# Data - Quarterly revenue by department (varied, non-monotonic pattern) |
| 13 | +categories = ["Engineering", "Marketing", "Sales", "Support", "Design", "Operations"] |
| 14 | +values = [38200, 21500, 45800, 14300, 27600, 19100] |
| 15 | +labels = [f"${v / 1000:.1f}K" for v in values] |
15 | 16 |
|
16 | 17 | # Create ColumnDataSource |
17 | | -source = ColumnDataSource(data={"categories": categories, "values": values}) |
| 18 | +source = ColumnDataSource(data={"categories": categories, "values": values, "labels": labels}) |
18 | 19 |
|
19 | | -# Create figure with categorical x-axis (4800 × 2700 px) |
| 20 | +# Create figure with categorical x-axis (4800 x 2700 px) |
20 | 21 | p = figure( |
21 | 22 | x_range=categories, |
22 | 23 | width=4800, |
23 | 24 | height=2700, |
24 | | - title="bar-basic · bokeh · pyplots.ai", |
25 | | - x_axis_label="Product Category", |
26 | | - y_axis_label="Sales ($)", |
| 25 | + title="bar-basic \u00b7 bokeh \u00b7 pyplots.ai", |
| 26 | + x_axis_label="Department", |
| 27 | + y_axis_label="Revenue ($)", |
27 | 28 | toolbar_location=None, |
28 | 29 | ) |
29 | 30 |
|
30 | | -# Create bars |
31 | | -p.vbar(x="categories", top="values", source=source, width=0.7, color="#306998", alpha=0.9) |
| 31 | +# Create bars with white edge for definition |
| 32 | +p.vbar( |
| 33 | + x="categories", top="values", source=source, width=0.7, color="#306998", alpha=0.9, line_color="white", line_width=2 |
| 34 | +) |
32 | 35 |
|
33 | | -# Add value labels on top of bars |
34 | | -labels = LabelSet( |
| 36 | +# Add formatted value labels above bars |
| 37 | +labels_glyph = LabelSet( |
35 | 38 | x="categories", |
36 | 39 | y="values", |
37 | | - text="values", |
| 40 | + text="labels", |
38 | 41 | level="glyph", |
39 | | - x_offset=-25, |
40 | | - y_offset=5, |
| 42 | + x_offset=0, |
| 43 | + y_offset=8, |
41 | 44 | source=source, |
42 | 45 | text_font_size="28pt", |
43 | 46 | text_color="#333333", |
| 47 | + text_align="center", |
44 | 48 | ) |
45 | | -p.add_layout(labels) |
| 49 | +p.add_layout(labels_glyph) |
46 | 50 |
|
47 | | -# Styling for 4800×2700 px |
| 51 | +# Styling for 4800x2700 px |
48 | 52 | p.title.text_font_size = "36pt" |
49 | 53 | p.title.align = "center" |
50 | 54 | p.xaxis.axis_label_text_font_size = "28pt" |
51 | 55 | p.yaxis.axis_label_text_font_size = "28pt" |
52 | 56 | p.xaxis.major_label_text_font_size = "24pt" |
53 | 57 | p.yaxis.major_label_text_font_size = "24pt" |
54 | 58 |
|
55 | | -# Grid styling |
| 59 | +# Clean L-shaped frame: remove top and right spines |
| 60 | +p.outline_line_color = None |
| 61 | +p.xaxis.axis_line_color = "#333333" |
| 62 | +p.yaxis.axis_line_color = "#333333" |
| 63 | +p.xaxis.axis_line_width = 2 |
| 64 | +p.yaxis.axis_line_width = 2 |
| 65 | + |
| 66 | +# Remove tick marks, keep labels |
| 67 | +p.xaxis.major_tick_line_color = None |
| 68 | +p.yaxis.major_tick_line_color = None |
| 69 | +p.xaxis.minor_tick_line_color = None |
| 70 | +p.yaxis.minor_tick_line_color = None |
| 71 | + |
| 72 | +# Grid styling - y-axis only, subtle solid lines |
56 | 73 | p.xgrid.grid_line_color = None |
57 | | -p.ygrid.grid_line_alpha = 0.3 |
58 | | -p.ygrid.grid_line_dash = "dashed" |
| 74 | +p.ygrid.grid_line_alpha = 0.2 |
| 75 | +p.ygrid.grid_line_dash = "solid" |
59 | 76 |
|
60 | | -# Y-axis starts at 0 |
| 77 | +# Format y-axis with dollar amounts |
| 78 | +p.yaxis.formatter = NumeralTickFormatter(format="$0,0") |
| 79 | + |
| 80 | +# Y-axis starts at 0, add headroom for labels |
61 | 81 | p.y_range.start = 0 |
| 82 | +p.y_range.end = max(values) * 1.15 |
| 83 | + |
| 84 | +# Background |
| 85 | +p.background_fill_color = "#FFFFFF" |
62 | 86 |
|
63 | 87 | # Save as PNG |
64 | 88 | export_png(p, filename="plot.png") |
|
0 commit comments