Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Matplotlib – Make a Frequency histogram from a list with tuple elements in Python
To make a frequency histogram from a list with tuple elements in Python, we can extract the categories and frequencies from tuples and create a bar chart using Matplotlib.
Steps to Create a Frequency Histogram
- Set the figure size and adjust the padding between and around the subplots
- Create a list of tuples containing category-frequency pairs
- Extract categories and frequencies by iterating through the data
- Create a bar plot using bar() method
- Display the figure using show() method
Example
Here's how to create a frequency histogram from tuple data ?
import matplotlib.pyplot as plt
# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Data as list of tuples (category, frequency)
data = [("a", 1), ("c", 3), ("d", 4), ("b", 2),
("e", 7), ("f", 3), ('g', 2)]
# Extract categories and frequencies
categories = []
frequencies = []
for item in data:
categories.append(item[0])
frequencies.append(item[1])
# Create bar plot
plt.bar(categories, frequencies)
plt.xlabel('Categories')
plt.ylabel('Frequency')
plt.title('Frequency Histogram from Tuple Data')
plt.show()
Alternative Method Using List Comprehension
You can also extract the data using list comprehension for more concise code ?
import matplotlib.pyplot as plt
# Data as list of tuples
data = [("Python", 15), ("Java", 12), ("C++", 8), ("JavaScript", 10)]
# Extract using list comprehension
categories = [item[0] for item in data]
frequencies = [item[1] for item in data]
# Create histogram
plt.figure(figsize=(8, 5))
plt.bar(categories, frequencies, color='skyblue', edgecolor='navy')
plt.xlabel('Programming Languages')
plt.ylabel('Popularity Score')
plt.title('Programming Language Popularity')
plt.show()
Using zip() Function
The most Pythonic way is to use the zip() function with unpacking ?
import matplotlib.pyplot as plt
# Sample data
data = [("Mon", 5), ("Tue", 8), ("Wed", 12), ("Thu", 7), ("Fri", 15)]
# Unpack using zip
categories, frequencies = zip(*data)
# Create styled histogram
plt.figure(figsize=(8, 5))
bars = plt.bar(categories, frequencies, color=['red', 'green', 'blue', 'orange', 'purple'])
plt.xlabel('Days of Week')
plt.ylabel('Sales Count')
plt.title('Weekly Sales Data')
plt.grid(axis='y', alpha=0.3)
# Add value labels on bars
for bar, freq in zip(bars, frequencies):
plt.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.1,
str(freq), ha='center', va='bottom')
plt.show()
Comparison of Methods
| Method | Code Length | Readability | Best For |
|---|---|---|---|
| For loop | Longer | Very clear | Beginners |
| List comprehension | Medium | Good | Intermediate users |
| zip() unpacking | Shortest | Excellent | Advanced users |
Conclusion
Creating frequency histograms from tuple data is straightforward with Matplotlib. Use zip(*data) for the most concise approach, or traditional loops for clarity. Add styling elements like colors, labels, and grids to make your histograms more informative.
Advertisements
