|
2 | 2 | Bar Chart |
3 | 3 | ========= |
4 | 4 |
|
5 | | -Demonstrate :meth:`~anyplotlib.figure_plots.Axes.bar` with vertical and |
6 | | -horizontal orientations, per-bar colours, category labels, and live data |
7 | | -updates via :meth:`~anyplotlib.figure_plots.PlotBar.update`. |
| 5 | +Demonstrate :meth:`~anyplotlib.figure_plots.Axes.bar` with: |
8 | 6 |
|
9 | | -Three separate figures are shown: |
10 | | -
|
11 | | -1. **Vertical bar chart** – monthly sales data with a uniform colour. |
12 | | -2. **Horizontal bar chart** – ranked items with per-bar colours and value |
13 | | - labels. |
14 | | -3. **Side-by-side comparison** – two panels sharing the same figure; one |
15 | | - panel updates its data to show a different quarter. |
| 7 | +* **Matplotlib-aligned API** — ``ax.bar(x, height, width, bottom, …)`` |
| 8 | +* Vertical and horizontal orientations, per-bar colours, category labels |
| 9 | +* **Grouped bars** — pass a 2-D *height* array ``(N, G)`` |
| 10 | +* **Log-scale value axis** — ``log_scale=True`` |
| 11 | +* Live data updates via :meth:`~anyplotlib.figure_plots.PlotBar.set_data` |
16 | 12 | """ |
17 | 13 | import numpy as np |
18 | 14 | import anyplotlib as vw |
19 | 15 |
|
20 | 16 | rng = np.random.default_rng(7) |
21 | 17 |
|
| 18 | +# ── 1. Vertical bar chart — monthly sales ──────────────────────────────────── |
| 19 | +# The first positional argument is now *x* (positions or labels), matching |
| 20 | +# ``matplotlib.pyplot.bar(x, height, width=0.8, bottom=0.0, ...)``. |
| 21 | +months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 22 | + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] |
| 23 | +sales = np.array([42, 55, 48, 63, 71, 68, 74, 81, 66, 59, 52, 78], |
| 24 | + dtype=float) |
| 25 | + |
| 26 | +fig1, ax1 = vw.subplots(1, 1, figsize=(640, 340)) |
| 27 | +bar1 = ax1.bar( |
| 28 | + months, # x — category strings become x_labels automatically |
| 29 | + sales, # height |
| 30 | + width=0.6, |
| 31 | + color="#4fc3f7", |
| 32 | + show_values=True, |
| 33 | + units="Month", |
| 34 | + y_units="Units sold", |
| 35 | +) |
| 36 | +fig1 |
| 37 | + |
| 38 | +# %% |
| 39 | +# Horizontal bar chart — ranked items |
| 40 | +# ------------------------------------- |
| 41 | +# Set ``orient="h"`` for a horizontal layout. Pass a list of CSS colours |
| 42 | +# to ``colors`` to give each bar its own colour. |
| 43 | + |
| 44 | +categories = ["NumPy", "SciPy", "Matplotlib", "Pandas", "Scikit-learn", |
| 45 | + "PyTorch", "TensorFlow", "JAX", "Polars", "Dask"] |
| 46 | +scores = np.array([95, 88, 91, 87, 83, 79, 76, 72, 68, 65], dtype=float) |
| 47 | + |
| 48 | +palette = [ |
| 49 | + "#ef5350", "#ec407a", "#ab47bc", "#7e57c2", "#42a5f5", |
| 50 | + "#26c6da", "#26a69a", "#66bb6a", "#d4e157", "#ffa726", |
| 51 | +] |
| 52 | + |
| 53 | +fig2, ax2 = vw.subplots(1, 1, figsize=(540, 400)) |
| 54 | +bar2 = ax2.bar( |
| 55 | + categories, |
| 56 | + scores, |
| 57 | + orient="h", |
| 58 | + colors=palette, |
| 59 | + width=0.65, |
| 60 | + show_values=True, |
| 61 | + y_units="Popularity score", |
| 62 | +) |
| 63 | +fig2 |
| 64 | + |
| 65 | +# %% |
| 66 | +# Grouped bar chart — quarterly comparison |
| 67 | +# ----------------------------------------- |
| 68 | +# Pass a 2-D *height* array of shape ``(N, G)`` to draw *G* bars side by |
| 69 | +# side for each category. Provide ``group_labels`` to show a legend and |
| 70 | +# ``group_colors`` to customise each group's colour. |
| 71 | + |
| 72 | +quarters = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"] |
| 73 | +q_data = np.array([ |
| 74 | + [42, 58, 51], # Jan — Q1, Q2, Q3 |
| 75 | + [55, 61, 59], # Feb |
| 76 | + [48, 70, 65], # Mar |
| 77 | + [63, 75, 71], # Apr |
| 78 | + [71, 69, 80], # May |
| 79 | + [68, 83, 77], # Jun |
| 80 | +], dtype=float) # shape (6, 3) → 6 categories, 3 groups |
| 81 | + |
| 82 | +fig3, ax3 = vw.subplots(1, 1, figsize=(680, 340)) |
| 83 | +bar3 = ax3.bar( |
| 84 | + quarters, |
| 85 | + q_data, |
| 86 | + width=0.8, |
| 87 | + group_labels=["Q1", "Q2", "Q3"], |
| 88 | + group_colors=["#4fc3f7", "#ff7043", "#66bb6a"], |
| 89 | + show_values=False, |
| 90 | + y_units="Sales", |
| 91 | +) |
| 92 | +fig3 |
| 93 | + |
| 94 | +# %% |
| 95 | +# Log-scale value axis |
| 96 | +# --------------------- |
| 97 | +# Set ``log_scale=True`` for a logarithmic value axis. Non-positive values |
| 98 | +# are clamped to ``1e-10`` — no error is raised. Tick marks are placed at |
| 99 | +# each decade (10⁰, 10¹, 10², …) with faint minor gridlines at 2×, 3×, 5× |
| 100 | +# multiples. |
| 101 | + |
| 102 | +log_labels = ["A", "B", "C", "D", "E"] |
| 103 | +log_vals = np.array([1, 10, 100, 1_000, 10_000], dtype=float) |
| 104 | + |
| 105 | +fig4, ax4 = vw.subplots(1, 1, figsize=(500, 340)) |
| 106 | +bar4 = ax4.bar( |
| 107 | + log_labels, |
| 108 | + log_vals, |
| 109 | + log_scale=True, |
| 110 | + color="#ab47bc", |
| 111 | + show_values=True, |
| 112 | + y_units="Count (log scale)", |
| 113 | +) |
| 114 | +fig4 |
| 115 | + |
| 116 | +# %% |
| 117 | +# Side-by-side comparison — update data live |
| 118 | +# ------------------------------------------- |
| 119 | +# Place two :class:`~anyplotlib.figure_plots.PlotBar` panels in one figure. |
| 120 | +# Call :meth:`~anyplotlib.figure_plots.PlotBar.set_data` to swap in Q2 data — |
| 121 | +# the value-axis range recalculates automatically. |
| 122 | + |
| 123 | +q1 = np.array([42, 55, 48, 63, 71, 68, 74, 81, 66, 59, 52, 78], dtype=float) |
| 124 | +q2 = np.array([58, 61, 70, 75, 69, 83, 90, 88, 77, 64, 71, 95], dtype=float) |
| 125 | +all_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 126 | + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] |
| 127 | + |
| 128 | +fig5, (ax_left, ax_right) = vw.subplots(1, 2, figsize=(820, 320)) |
| 129 | +bar_left = ax_left.bar( |
| 130 | + all_months, q1, width=0.6, |
| 131 | + color="#4fc3f7", show_values=False, y_units="Q1 sales", |
| 132 | +) |
| 133 | +bar_right = ax_right.bar( |
| 134 | + all_months, q1, width=0.6, |
| 135 | + color="#ff7043", show_values=False, y_units="Q2 sales", |
| 136 | +) |
| 137 | +bar_right.set_data(q2) # swap in Q2 — axis range recalculates automatically |
| 138 | + |
| 139 | +fig5 |
| 140 | + |
| 141 | +# %% |
| 142 | +# Mutate colours, annotations, and scale at runtime |
| 143 | +# -------------------------------------------------- |
| 144 | +# :meth:`~anyplotlib.figure_plots.PlotBar.set_color` repaints all bars, |
| 145 | +# :meth:`~anyplotlib.figure_plots.PlotBar.set_show_values` toggles labels, |
| 146 | +# :meth:`~anyplotlib.figure_plots.PlotBar.set_log_scale` switches the |
| 147 | +# value-axis between linear and logarithmic. |
| 148 | + |
| 149 | +bar1.set_color("#ff7043") |
| 150 | +bar1.set_show_values(False) |
| 151 | +fig1 |
| 152 | + |
| 153 | +import numpy as np |
| 154 | +import anyplotlib as vw |
| 155 | + |
| 156 | +rng = np.random.default_rng(7) |
| 157 | + |
22 | 158 | # ── 1. Vertical bar chart — monthly sales ──────────────────────────────────── |
23 | 159 | months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", |
24 | 160 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] |
|
70 | 206 | # ------------------------------------------- |
71 | 207 | # Place two :class:`~anyplotlib.figure_plots.PlotBar` panels in one |
72 | 208 | # :func:`~anyplotlib.figure_plots.subplots` figure. Call |
73 | | -# :meth:`~anyplotlib.figure_plots.PlotBar.update` to swap in Q2 data for the |
| 209 | +# :meth:`~anyplotlib.figure_plots.PlotBar.set_data` to swap in Q2 data for the |
74 | 210 | # right panel, demonstrating how the axis range re-calculates automatically. |
75 | 211 |
|
76 | 212 | quarters = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", |
|
100 | 236 | ) |
101 | 237 |
|
102 | 238 | # Swap in Q2 data — range is recalculated automatically |
103 | | -bar_right.update(q2) |
| 239 | +bar_right.set_data(q2) |
104 | 240 |
|
105 | 241 | fig3 |
106 | 242 |
|
|
0 commit comments