Matplotlib Ref Bar
* * Matplotlib Reference](#)\\n\\n`bar()` is used to draw vertical bar charts, and `barh()` is used to draw horizontal bar charts.\\n\\nBar charts are used to show the quantitative comparison of categorical data, where each category is represented by a bar whose length corresponds to its value.\\n\\n## Function Definition\\n\\n### bar() - Vertical Bar Chart\\n\\nmatplotlib.pyplot.bar(x, height, width=0.8, bottom=0, *, align='center', **kwargs)\\n### barh() - Horizontal Bar Chart\\n\\nmatplotlib.pyplot.barh(y, width, height=0.8, left=0, *, align='center', **kwargs)\\n### Axes Interface\\n\\nAxes.bar(x, height, width=0.8, bottom=0, *, align='center', **kwargs)Axes.barh(y, width, height=0.8, left=0, *, align='center', **kwargs)\\n## Parameter Description\\n\\n| Parameter | Type | Description |\\n| --- | --- | --- |\\n| x / y | array-like | Position coordinates of the bars (x for vertical, y for horizontal) |\\n| height / width | array-like | Height of the bars (vertical) or width (horizontal), i.e., the data values |\\n| width (bar) / height (barh) | float or array-like | Width/height of the bars, default 0.8 |\\n| bottom / left | float or array-like | Starting position of the bar base, default 0. Can be used to draw stacked bar charts |\\n| align | str | Alignment of the bars relative to the x/y coordinates: 'center' (default) or 'edge' |\\n| color | color or list | Bar color, can be a single color or a list of colors |\\n| edgecolor | color | Bar border color |\\n| linewidth / lw | float | Bar border width |\\n| tick_label | array-like | Tick labels for the bars, corresponding one-to-one with the x positions |\\n| label | str | Legend label (used for grouped bar charts) |\\n| alpha | float | Transparency |\\n| hatch | str | Fill pattern: '/', '', '|', '-', '+', 'x', 'o', '*' |\\n\\n## bar_label() Supplementary Description\\n\\n`bar_label()` is used to display value labels directly on the bars.\\n\\nAxes.bar_label(container, labels=None, *, fmt=None, label_type='edge', padding=0, **kwargs)\\n> bar() and barh() return a `BarContainer` object, which can be passed to bar_label() to add value labels. This is the recommended approach in Matplotlib 3.4+.\\n\\n* * *\\n\\n## Usage Examples\\n\\n### Example 1: Basic Vertical Bar Chart\\n\\n## Instance\\n\\nimport matplotlib.pyplot as plt\\n\\n# Category Labels and Data\\n\\n categories =['Python','Java','JavaScript','C++','Go']\\n\\n values =[85,72,68,55,42]\\n\\nfig, ax = plt.subplots(layout='constrained')\\n\\n# Plot Bar Chart\\n\\n bars = ax.bar(categories, values,\\n\\n color=['#3498db','#e74c3c','#f39c12','#2ecc71','#9b59b6'],\\n\\n edgecolor='white', linewidth=1)\\n\\n# Display Values on Bars\\n\\n ax.bar_label(bars, fmt='%d', padding=3)\\n\\nax.set_title('Programming Language Popularity')\\n\\n ax.set_ylabel('Score')\\n\\n ax.set_ylim(0,100)\\n\\n ax.grid(axis='y', alpha=0.3)\\n\\n plt.show()\\n\\n### Example 2: Grouped Bar Chart\\n\\n## Instance\\n\\nimport matplotlib.pyplot as plt\\n\\nimport numpy as np\\n\\n# Grouped Data\\n\\n labels =['Q1','Q2','Q3','Q4']\\n\\n product_a =[120,135,148,162]\\n\\n product_b =[90,105,115,130]\\n\\nx = np.arange(len(labels))# x Position:[0, 1, 2, 3]\\n\\n width =0.35# Width of Each Bar\\n\\nfig, ax = plt.subplots(layout='constrained')\\n\\n# First Group of Bars (Offset -width/2)\\n\\n bars1 = ax.bar(x - width/2, product_a, width,\\n\\nlabel='Product A', color='steelblue', edgecolor='white')\\n\\n# Second Group of Bars (Offset +width/2)\\n\\n bars2 = ax.bar(x + width/2, product_b, width,\\n\\nlabel='Product B', color='coral', edgecolor='white')\\n\\n# Add Value Labels\\n\\n ax.bar_label(bars1, padding=3, fmt='%d')\\n\\n ax.bar_label(bars2, padding=3, fmt='%d')\\n\\nax.set_title('Quarterly Sales Comparison')\\n\\n ax.set_ylabel('Sales (units)')\\n\\n ax.set_xticks(x)\\n\\n ax.set_xticklabels(labels)\\n\\n ax.legend()\\n\\n ax.grid(axis='y', alpha=0.3)\\n\\n plt.show()\\n\\n### Example 3: Stacked Bar Chart\\n\\n## Instance\\n\\nimport matplotlib.pyplot as plt\\n\\ncategories =['Jan','Feb','Mar','Apr','May']\\n\\n income =[5000,5200,4800,5500,6000]\\n\\n expense =[3000,3500,3200,4000,4200]\\n\\nfig, ax = plt.subplots(layout='constrained')\\n\\n# First Layer of Bars (Bottom)\\n\\n bars_income = ax.bar(categories, income,\\n\\nlabel='Income', color='#2ecc71', edgecolor='white')\\n\\n# Second Layer of Bars (Stacked on Top of First Layer)\\n\\n bars_expense = ax.bar(categories, expense, bottom=income,\\n\\n label='Expense', color='#e74c3c', edgecolor='white')\\n\\n# Add Labels\\n\\n ax.bar_label(bars_income, label_type='center', fmt='$%d')\\n\\n ax.bar_label(bars_expense, label_type='center', fmt='$%d')\\n\\nax.set_title('Stacked Bar Chart: Monthly Finance')\\n\\n ax.set_ylabel('Amount ($)')\\n\\n ax.legend()\\n\\n ax.grid(axis='y', alpha=0.3)\\n\\n plt.show()\\n\\n### Example 4: Horizontal Bar Chart (barh)\\n\\n## Instance\\n\\nimport matplotlib.pyplot as plt\\n\\ntasks =['Task A','Task B','Task C','Task D','Task E']\\n\\n hours =[12.5,8.0,15.3,6.2,10.8]\\n\\nfig, ax = plt.subplots(figsize=(8,4), layout='constrained')\\n\\n# Horizontal Bar Chart\\n\\n bars = ax.barh(tasks, hours,\\n\\ncolor='#3498db', edgecolor='white', height=0.6)\\n\\n# Add Labels at the End of Bars\\n\\n ax.bar_label(bars, fmt='%.1f h', padding=5)\\n\\nax.set_title('Task Completion Time (Horizontal Bar)')\\n\\n ax.set_xlabel('Hours')\\n\\n ax.invert_yaxis()# Invert y axis, so that the first category is at the top\\n\\n ax.grid(axis='x', alpha=0.3)\\n\\n plt.show()\\n\\n* * *\\n\\n## Frequently Asked Questions\\n\\n### What is the axis correspondence between bar() and barh()?\\n\\n`bar(x, height)`: x is the position, height determines the bar length (vertical direction).\\n\\n`barh(y, width)`: y is the position, width determines the bar length (horizontal direction).\\n\\nNote the different parameter names: bar uses height, while barh uses width to represent the data values.\\n\\n### How to make the bars in a grouped bar chart sit closely together?\\n\\nSet `width` to the reciprocal of the total width of each group of bars, and use `align='edge'`.\\n\\n[ Matplotlib Reference](#)
YouTip