Data visualization is an essential aspect of data analysis, allowing us to interpret complex datasets visually. Matplotlib is one of the most widely used libraries in Python for creating static, animated, and interactive visualizations. Below are three diverse examples demonstrating how to effectively use Matplotlib for data visualization.
A line chart is useful for showing trends over time. This example illustrates how to visualize monthly sales data for a company.
import matplotlib.pyplot as plt
import numpy as np
# Sample data
months = np.array(['January', 'February', 'March', 'April', 'May', 'June'])
sales = np.array([15000, 23000, 18000, 25000, 30000, 35000])
# Create the line chart
plt.figure(figsize=(10, 5))
plt.plot(months, sales, marker='o')
plt.title('Monthly Sales Data')
plt.xlabel('Months')
plt.ylabel('Sales in USD')
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
marker='o'
parameter adds circular markers to each data point for better visibility.figsize
helps in customizing the size of the plot for better presentation.Bar charts are effective for comparing quantities across different categories. This example shows the number of products sold across various categories.
import matplotlib.pyplot as plt
import numpy as np
# Sample data
categories = ['Electronics', 'Clothing', 'Toys', 'Books', 'Games']
sold_units = [1200, 800, 450, 600, 300]
# Create the bar chart
plt.figure(figsize=(10, 6))
plt.bar(categories, sold_units, color='skyblue')
plt.title('Products Sold by Category')
plt.xlabel('Categories')
plt.ylabel('Units Sold')
plt.xticks(rotation=30)
plt.tight_layout()
plt.show()
color
parameter customizes the color of the bars.xticks(rotation=30)
improves readability when category names are long.Scatter plots are essential for determining relationships between two variables. In this example, we visualize the correlation between study hours and exam scores.
import matplotlib.pyplot as plt
import numpy as np
# Sample data
study_hours = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
exam_scores = np.array([50, 55, 60, 65, 70, 75, 80, 85, 90, 95])
# Create the scatter plot
plt.figure(figsize=(10, 6))
plt.scatter(study_hours, exam_scores, color='purple', s=100, alpha=0.7)
plt.title('Study Hours vs Exam Scores')
plt.xlabel('Study Hours')
plt.ylabel('Exam Scores')
plt.grid(True)
plt.tight_layout()
plt.show()
s=100
parameter sets the size of the points in the scatter plot, while alpha=0.7
adjusts transparency.By utilizing these examples of data visualization with Matplotlib in Python, you can create insightful and informative visual representations of your data.