Bar Charts

Loading concept...

📊 Bar Charts: Building Stories with Blocks!

🎬 The Big Picture

Imagine you have a box of colorful building blocks. Each block represents something—like how many cookies you ate each day, or how many goals your friends scored. Bar charts are like lining up your blocks to see who has the tallest tower!

That’s exactly what Matplotlib bar charts do. They turn boring numbers into colorful towers you can compare at a glance.


đź§± Bar Chart Basics

What’s a Bar Chart?

Think of a bar chart like a race where everyone stands on platforms. The taller your platform, the bigger your number!

import matplotlib.pyplot as plt

fruits = ['Apple', 'Banana', 'Orange']
count = [4, 7, 3]

plt.bar(fruits, count)
plt.title('My Fruit Count')
plt.show()

What happens here?

  • fruits = names on the ground (x-axis)
  • count = how tall each bar grows (y-axis)
  • plt.bar() = the magic that draws the towers
graph TD A[Your Data] --> B[plt.bar] B --> C[Beautiful Bars!]

🍎 Simple Example

You counted your toys:

  • Cars: 5
  • Dolls: 3
  • Blocks: 8

The bar chart shows: Blocks win! The tallest bar = the most toys.


↔️ Horizontal Bar Chart

Turning Sideways!

Sometimes names are too long. Like “Chocolate Chip Cookie” vs “Cookie”. Solution? Flip the chart sideways!

import matplotlib.pyplot as plt

snacks = ['Chips', 'Cookies',
          'Candy', 'Fruit']
votes = [12, 18, 8, 15]

plt.barh(snacks, votes)
plt.title('Favorite Snacks')
plt.xlabel('Votes')
plt.show()

The secret: Use plt.barh() instead of plt.bar()

The “h” stands for horizontal—bars grow sideways like lazy cats stretching!

When to Use Horizontal Bars?

Use Vertical Use Horizontal
Short labels Long labels
Few items Many items
Time on x-axis Ranking lists

👥 Grouped Bar Chart

Side-by-Side Friends!

What if you want to compare TWO things for each category? Like comparing Monday’s ice cream sales vs Tuesday’s?

Grouped bars stand next to each other like best friends!

import matplotlib.pyplot as plt
import numpy as np

days = ['Mon', 'Tue', 'Wed']
x = np.arange(len(days))
width = 0.35

vanilla = [5, 7, 4]
chocolate = [6, 4, 8]

plt.bar(x - width/2, vanilla,
        width, label='Vanilla')
plt.bar(x + width/2, chocolate,
        width, label='Chocolate')

plt.xticks(x, days)
plt.legend()
plt.show()

The trick:

  1. Create positions with np.arange()
  2. Shift first bars left: x - width/2
  3. Shift second bars right: x + width/2
graph TD A[Position Numbers] --> B[Shift Left Group] A --> C[Shift Right Group] B --> D[Side by Side!] C --> D

📚 Stacked Bar Chart

Building Towers on Top!

What if you want to show parts of a whole? Like how many red, blue, and green candies you have?

Stacked bars pile on top of each other like a candy tower!

import matplotlib.pyplot as plt

kids = ['Ana', 'Ben', 'Cat']
red = [3, 5, 2]
blue = [4, 2, 6]
green = [2, 3, 4]

plt.bar(kids, red, label='Red')
plt.bar(kids, blue, bottom=red,
        label='Blue')

# For green, we need red + blue
import numpy as np
bottom2 = np.array(red) + np.array(blue)
plt.bar(kids, green, bottom=bottom2,
        label='Green')

plt.legend()
plt.title('Candy Collection')
plt.show()

The magic word: bottom=

This tells the bar: “Start from HERE, not from zero!”

Stacking Rules

  1. First bar: No bottom needed
  2. Second bar: bottom=first_bar_values
  3. Third bar: bottom=first + second

🎨 Bar Chart Customization

Making Bars Beautiful!

Plain bars are boring. Let’s add some sparkle!

Colors

colors = ['red', 'gold', 'skyblue']
plt.bar(fruits, count, color=colors)

Or use one color for all:

plt.bar(fruits, count, color='coral')

Edge Style

plt.bar(fruits, count,
        color='lightblue',
        edgecolor='navy',
        linewidth=2)

Bar Width

plt.bar(fruits, count, width=0.5)
# Default is 0.8
# Smaller = thinner bars
# Bigger = fatter bars

Adding Labels on Bars

bars = plt.bar(fruits, count)

for bar in bars:
    height = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2,
             height + 0.1,
             str(int(height)),
             ha='center')

This puts numbers on TOP of each bar!

All Together Example

import matplotlib.pyplot as plt

fruits = ['🍎', '🍌', '🍊', '🍇']
count = [4, 7, 3, 5]

bars = plt.bar(fruits, count,
               color=['red', 'yellow',
                      'orange', 'purple'],
               edgecolor='black',
               linewidth=1.5,
               width=0.6)

# Add value labels
for bar in bars:
    h = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2,
             h + 0.2, str(h), ha='center',
             fontweight='bold')

plt.title('My Fruit Basket',
          fontsize=14, fontweight='bold')
plt.ylabel('Count')
plt.ylim(0, 9)
plt.show()

🗺️ Quick Reference Map

graph TD A[Bar Charts] --> B[Basic: plt.bar] A --> C[Horizontal: plt.barh] A --> D[Grouped: Side-by-side] A --> E[Stacked: bottom=] A --> F[Customize!] F --> G[color] F --> H[width] F --> I[edgecolor] F --> J[labels]

🎯 Remember This!

Chart Type When to Use Key Code
Basic Compare items plt.bar(x, y)
Horizontal Long labels plt.barh(x, y)
Grouped Compare groups Shift with width
Stacked Show parts Use bottom=

🚀 You Did It!

You now know how to:

  • âś… Create basic bar charts
  • âś… Flip them sideways
  • âś… Group bars for comparison
  • âś… Stack bars to show parts
  • âś… Make them look amazing!

Bar charts are like building blocks for data. Stack them, line them up, color them—and tell stories that everyone can see and understand!

Now go make some beautiful charts! 🎨📊

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.