0% found this document useful (0 votes)
17 views12 pages

Bar Plot

A bar plot is a visual representation of data using rectangular bars, where each bar corresponds to a category and its length or height indicates the value or frequency of that category. Bar charts are useful for comparing different categories and should be used when showing distributions or comparisons across subgroups. Best practices include using a zero-valued baseline, maintaining rectangular forms, using colors wisely, and including value annotations.

Uploaded by

243803
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views12 pages

Bar Plot

A bar plot is a visual representation of data using rectangular bars, where each bar corresponds to a category and its length or height indicates the value or frequency of that category. Bar charts are useful for comparing different categories and should be used when showing distributions or comparisons across subgroups. Best practices include using a zero-valued baseline, maintaining rectangular forms, using colors wisely, and including value annotations.

Uploaded by

243803
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

What is Bar Plot ?

• A bar graph (or bar chart) is a visual way of presenting data using
rectangular bars.
• Each bar represents a category
• length or height of the bar corresponds to the value or frequency
of that category.

What are the key feature of a bar graph ?


• Bars can be vertical or horizontal.
• Used to compare different categories or groups.
• The x-axis usually shows categories.
• The y-axis usually shows numerical values.

When you should use a bar chart ?


• A bar chart is used when you want to show a distribution of data
points
• or perform a comparison of metric values across different
subgroups of your data.
• From a bar chart, we can see which groups are highest or most
common, and how other groups compare against the others.
• Since this is a fairly common task, bar charts are a fairly
ubiquitous chart type.
• The primary variable is categorical variable.
• A categorical variable takes discrete values, which can be
thought of as labels.
• Examples include state or country, industry type, website access
method (desktop, mobile), and visitor type (free, basic, premium).
• Some categorical variables have ordered values, like dividing
objects by size (small, medium, large).
• In addition, some non-categorical variables can be converted into
groups, like aggregating temporal data based on date
• The important point for this primary variable is that the groups
are distinct.
• In contrast, the secondary variable will be numeric in nature.
• The secondary variable’s values determine the length of each
bar.
• These values can come from a great variety of sources. In its
simplest form, the values may be a simple frequency count or
proportion for how much of the data is divided into each category
– not an actual data feature at all.
• For example, the following plot counts pageviews over a period
of six months.
• You can see from this visualization that there was a small peak in
June and July before returning to the previous baseline.
import matplotlib.pyplot as plt

# Sample data: Page views over six months


months = ['Nov', 'Dec', 'Jan', 'Feb', 'Mar', 'Apr']
page_views = [1500, 1800, 1700, 1600, 2000, 2200]

# Create a bar graph


plt.figure(figsize=(8, 4))
plt.bar(months, page_views, alpha=1)
plt.title('Page Views Over Six Months In 2023')
plt.xlabel('Month')
plt.ylabel('Page Views')
plt.grid(axis='y', linestyle='--', alpha=1, color='black')
plt.tight_layout()

# Show the plot


plt.show()
import matplotlib.pyplot as plt

# Data lists
classes = []
students = []

# Input loop
while True:
cls = input("Enter the class (or type 'exit' to finish): ")
if cls.lower() == 'exit':
break
std_count = int(input("Number of students: "))

classes.append(cls)
students.append(int(std_count))

# Plotting
plt.bar(classes, students)
plt.title('Number of Students per Class')
plt.xlabel('Class')
plt.ylabel('Number of Students')
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.tight_layout()
plt.show()
Best practices for using bar charts
Use a common zero-valued baseline First and foremost, make sure that all of your bars are being
plotted against a zero-value baseline. Not only does that baseline make it easier for readers to
compare bar lengths, it also maintains the truthfulness of your data visualization. A bar chart
with a non-zero baseline or some other gap in the axis scale can easily misrepresent comparison
between groups since the ratio in bar lengths will not match the ratio in actual bar values.

import matplotlib.pyplot as plt

# Data
categories = ['Group A', 'Group B']
values = [100, 110]

# Create subplots to compare proper vs misleading bar charts


fig, axes = plt.subplots(1, 2, figsize=(10, 5))

# Proper bar chart (starts at zero)


axes[0].bar(categories, values)
axes[0].set_title('Correct Bar Chart\n(Zero Baseline)')
axes[0].set_ylim(0, max(values) + 20)
axes[0].set_ylabel('Value')
axes[0].grid(axis='y', linestyle="--",color='black', alpha=0.5)

# Misleading bar chart (starts at 90)


axes[1].bar(categories, values)
axes[1].set_title('Misleading Bar Chart\n(Starts at 90)')
axes[1].set_ylim(90, max(values) + 5)
axes[1].set_ylabel('Value')
axes[1].grid(axis='y', linestyle="--",color='black', alpha=0.5)
plt.tight_layout()
plt.show()
Maintain rectangular forms for your bars

Use color wisely


import matplotlib.pyplot as plt

# Data
animals = ['Elephants', 'Tigers', 'Lions', 'Zebras', 'Giraffes']
population = [500, 300, 200, 450, 350]
colors = ['gray', 'orange', 'gold', 'black', 'brown']

# Plot
plt.figure(figsize=(8, 5))
plt.barh(animals, population, color=colors)
plt.title('Wild Animal Populations')
plt.xlabel('Animals')
plt.ylabel('Population')
plt.tight_layout()
plt.show()
# Custom coloring: only Tigers and Lions get colors, rest are
lightgray
highlight_colors = ['lightgray', 'orange', 'gold', 'lightgray',
'lightgray']

plt.figure(figsize=(8, 5))
plt.barh(animals, population, color=highlight_colors)
plt.title('Highlighting Tigers and Lions')
plt.xlabel('Animals')
plt.ylabel('Population')
plt.tight_layout()
plt.show()
Include value annotations
import matplotlib.pyplot as plt

# Data
animals = ['Elephants', 'Tigers', 'Lions', 'Zebras', 'Giraffes']
population = [500, 300, 200, 450, 350]
colors = ['gray', 'orange', 'gold', 'black', 'brown']

# Create bar chart


bars = plt.bar(animals, population, color=colors)

# Add labels and title


plt.xlabel('Classes')
plt.ylabel('Number of Students')
plt.title('Number of Students in Each Class')

# Add the values in the center of the bars


for bar in bars:
yval = bar.get_height() # Get the height (value) of the bar
plt.text(bar.get_x() + bar.get_width()/2, yval/2, str(yval),
ha='center', va='center', fontsize=12, color='white')
print(bar.get_x() + bar.get_width()/2, yval/2, str(yval))
# Show the plot
plt.show()
0.0 250.0 500
1.0 150.0 300
2.0 100.0 200
3.0 225.0 450
4.0 175.0 350

Include variability whiskers


import matplotlib.pyplot as plt

# Data
animals = ['Elephants', 'Tigers', 'Lions', 'Zebras', 'Giraffes']
population = [500, 300, 200, 450, 350]
lower_errors = [10, 10, 10, 10, 10]
upper_errors = [5, 5, 5, 5, 5]
errors = [lower_errors, upper_errors]
colors = ['gray', 'orange', 'gold', 'brown', 'silver']

# Create bar chart


bars = plt.bar(animals, population, yerr =errors, color=colors)
# Add labels and title
plt.xlabel('Classes')
plt.ylabel('Number of Students')
plt.title('Number of Students in Each Class')

# Add the values in the center of the bars


for bar in bars:
yval = bar.get_height() # Get the height (value) of the bar
plt.text(bar.get_x() + bar.get_width()/2, yval/2, str(yval),
ha='center', va='center', fontsize=12, color='white')
print(bar.get_x() + bar.get_width()/2, yval/2, str(yval))
# Show the plot
plt.show()

0.0 250.0 500


1.0 150.0 300
2.0 100.0 200
3.0 225.0 450
4.0 175.0 350
Stacked bar chart and grouped bar chart
import matplotlib.pyplot as plt
import numpy as np

# Data lists
classes = []
boys = []
girls = []

# Input loop
while True:
cls = input("Enter the class (or type 'exit' to finish): ")
if cls.lower() == 'exit':
break
b = int(input(f"Number of boys in {cls}: "))
g = int(input(f"Number of girls in {cls}: "))

classes.append(cls)
boys.append(b)
girls.append(g)

# Positioning bars
x = np.arange(len(classes)) # class positions on x-axis
width = 0.35 # width of each bar

# Plotting
plt.figure(figsize=(10, 6))
plt.bar(x - width/2, boys, width, label='Boys', color='brown')
plt.bar(x + width/2, girls, width, label='Girls', color='orange')

plt.xlabel('Class')
plt.ylabel('Number of Students')
plt.title('Number of Boys and Girls per Class')
plt.xticks(x, classes)
plt.legend()
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.tight_layout()
plt.show()

You might also like