SRGI, BHILAI
UNIT – 5
Visualization with Matplotlib
Introduction to Visualization with Matplotlib
Data visualization is essential in data analytics, as it helps to interpret
complex data more intuitively. The matplotlib library in Python is widely used
for creating static, animated, and interactive visualizations. Here's a detailed
guide with examples covering various aspects of data visualization with
matplotlib, including different chart types, customization, and saving your
visualizations.
1. Data Visualization with matplotlib: A Simple Interactive Chart
To start, install matplotlib if you haven't already:
pip install matplotlib
Basic Example: A Line Chart
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Creating a basic line plot
plt.plot(x, y, marker='o') # 'marker' adds points on the line
plt.title("Simple Interactive Line Chart")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
This chart allows you to plot values and provides a starting point for adding more
interactive elements.
SRGI, BHILAI
2. Set the Properties of the Plot
You can set properties such as line style, color, and marker to make the chart more
readable.
plt.plot(x, y, color='green', linestyle='--', marker='o', markersize=8, linewidth=2)
plt.title("Customized Line Chart", fontsize=14, color='blue')
plt.xlabel("X-axis", fontsize=12)
plt.ylabel("Y-axis", fontsize=12)
plt.grid(True) # Adds a grid for better readability
plt.show()
Explanation:
• color='green': Sets the line color.
• linestyle='--': Makes the line dashed.
• marker='o': Adds markers at each data point.
• markersize=8: Sets marker size.
• linewidth=2: Sets line thickness.
• fontsize and color adjust the axis and title fonts.
3. matplotlib and NumPy
NumPy integrates seamlessly with matplotlib, enabling mathematical operations
directly on datasets.
import numpy as np
# Generating data with numpy
x = np.linspace(0, 10, 100) # 100 points between 0 and 10
y = np.sin(x)
plt.plot(x, y, label='sin(x)')
plt.title("Sinusoidal Function")
plt.xlabel("X values")
SRGI, BHILAI
plt.ylabel("sin(X)")
plt.legend()
plt.show()
Explanation:
• np.linspace(0, 10, 100): Creates an array of 100 points between 0 and 10.
• np.sin(x): Applies the sine function to each value in x.
4. Working with Multiple Figures and Axes
In data analysis, you may want to plot multiple charts on the same figure or in
separate figures.
Multiple Figures
# First figure
plt.figure(1)
plt.plot(x, y, 'r') # Red line for sin(x)
plt.title("First Figure")
# Second figure
plt.figure(2)
plt.plot(x, np.cos(x), 'b') # Blue line for cos(x)
plt.title("Second Figure")
plt.show()
SRGI, BHILAI
Multiple Axes in One Figure
fig, axs = plt.subplots(2) # 2 rows
axs[0].plot(x, np.sin(x), 'r')
axs[0].set_title("Sine Wave")
axs[1].plot(x, np.cos(x), 'b')
axs[1].set_title("Cosine Wave")
plt.tight_layout() # Adjust spacing between plots
plt.show()
5. Adding Text
Adding text annotations helps highlight key points in the data.
plt.plot(x, y, 'g')
plt.title("Annotated Sinusoidal Chart")
plt.text(5, 0, "Middle Point", fontsize=12, color='purple') # Add text at
coordinates (5,0)
plt.show()
SRGI, BHILAI
Explanation:
• plt.text(x, y, "text"): Adds a text label at the specified (x, y) coordinates.
6. Adding a Grid
Adding a grid improves readability, especially for larger datasets.
plt.plot(x, y, 'b')
plt.title("Chart with Grid")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True) # Add grid to the chart
plt.show()
7. Adding a Legend
A legend identifies different series on a chart, useful when plotting multiple lines.
plt.plot(x, y, label="sin(x)")
plt.plot(x, np.cos(x), label="cos(x)", linestyle='--')
plt.title("Sine and Cosine Waves")
SRGI, BHILAI
plt.xlabel("X values")
plt.ylabel("Y values")
plt.legend(loc="upper right") # Position the legend
plt.show()
Explanation:
• label="text" assigns a label to each line.
• plt.legend(loc="upper right") places the legend at the top right.
8. Saving the Charts
You can save plots as images using savefig.
plt.plot(x, y, 'b')
plt.title("Saved Chart Example")
plt.savefig("saved_chart.png", dpi=300, format='png') # Save as PNG with high
resolution
plt.show()
Explanation:
• dpi=300 specifies the resolution.
• format='png' saves as a PNG file.
SRGI, BHILAI
9. Different Chart Types
Line Chart
Line charts are useful for showing trends over continuous data.
plt.plot(x, y, color='blue', linestyle='-', marker='o')
plt.title("Line Chart")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Histogram
Histograms are used to show the distribution of a dataset.
data = np.random.randn(1000) # Random data
plt.hist(data, bins=30, color='green', alpha=0.7)
plt.title("Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
Explanation:
• bins=30: Divides the data range into 30 intervals.
• alpha=0.7: Sets transparency.
SRGI, BHILAI
Bar Chart
Bar charts are used to compare categories.
categories = ['A', 'B', 'C', 'D']
values = [10, 15, 7, 10]
plt.bar(categories, values, color='purple')
plt.title("Bar Chart")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()
Pie Chart
Pie charts are useful for showing the proportion of categories in a dataset.
sizes = [15, 30, 45, 10]
labels = ['Category A', 'Category B', 'Category C', 'Category D']
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140)
plt.title("Pie Chart")
plt.show()
SRGI, BHILAI
Explanation:
• autopct='%1.1f%%': Displays percentage values on the chart.
• startangle=140: Rotates the chart for a better view.
Summary
This guide covers:
1. Line Charts for trends.
2. Histograms for distributions.
3. Bar Charts for category comparisons.
4. Pie Charts for proportions.
5. Customization Options such as grids, legends, annotations, and multiple
figures.
matplotlib is a powerful library, enabling rich, customizable visualizations. The
above examples should provide a foundation for creating effective data
visualizations in Python.