Bar Plot
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.
# 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.
# Data
categories = ['Group A', 'Group B']
values = [100, 110]
# 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']
# 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']
# 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()