Matplotlib is a powerful Python library for data visualization. It lets you create high-quality 2D plots such as line graphs, bar charts, scatter plots, histograms, and pie charts. It is widely used in data analysis, scientific computing, and machine learning to understand and present data visually.
plt) is the main interface for plotting.plot(), bar(), scatter(), hist(), and pie().The typical Matplotlib workflow looks like this:
import matplotlib.pyplot as pltplt.show().
import matplotlib.pyplot as plt
# 1. Prepare data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# 2. Create plot
plt.plot(x, y)
# 3. Customize plot
plt.title("Simple Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# 4. Display plot
plt.show()
Install Matplotlib using pip from your terminal or command prompt:
# Install matplotlib using pip
pip install matplotlib
The simplest visualization is a line plot using plt.plot() and plt.show().
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a line plot
plt.plot(x, y)
plt.title("Simple Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Bar charts are used to compare values across categories.
import matplotlib.pyplot as plt
# Define categories and their values
categories = ['A', 'B', 'C', 'D']
values = [10, 20, 15, 25]
# Create and display bar chart
plt.bar(categories, values, color='skyblue')
plt.title("Bar Chart Example")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()
Scatter plots show the relationship between two numeric variables.
import matplotlib.pyplot as plt
# Sample x and y data points
x = [5, 7, 8, 7, 2, 17, 2, 9]
y = [99, 86, 87, 88, 100, 86, 103, 87]
# Create scatter plot and show it
plt.scatter(x, y, color='red', marker='o')
plt.title("Scatter Plot Example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
You can change colors, line styles, markers, grids, and add legends to make plots more informative.
import matplotlib.pyplot as plt
# Define x and y values
x = [1, 2, 3, 4, 5]
y = [10, 5, 8, 12, 7]
# Plot with custom style and labels
plt.plot(x, y, color='green', linestyle='--', marker='o', label='Line 1')
plt.title("Customized Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# Add grid, legend, and show plot
plt.grid(True)
plt.legend()
plt.show()
Subplots let you show multiple related plots side by side in a single figure.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y1 = [2, 3, 5, 7, 11]
y2 = [1, 4, 6, 8, 10]
plt.subplot(1, 2, 1) # 1 row, 2 columns, 1st plot
plt.plot(x, y1, 'r-o')
plt.title("Plot 1")
plt.subplot(1, 2, 2) # 2nd plot
plt.plot(x, y2, 'b-s')
plt.title("Plot 2")
plt.tight_layout()
plt.show()
Histograms display the frequency distribution of numerical data.
import matplotlib.pyplot as plt
# Sample data for the histogram
data = [1,2,2,3,3,3,4,4,4,4,5,5,5,6]
# Plot histogram and label axes
plt.hist(data, bins=6, color='purple', edgecolor='black')
plt.title("Histogram Example")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
Pie charts represent parts of a whole as slices of a circle.
import matplotlib.pyplot as plt
# Define slice sizes and labels
sizes = [30, 25, 20, 25]
labels = ['A', 'B', 'C', 'D']
# Draw the pie chart and show percentages
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.title("Pie Chart Example")
plt.show()
y increases as x increases.plt.tight_layout() when working with subplots to avoid overlapping elements.matplotlib.pyplot.style.use('ggplot') or other styles for a quick visual upgrade.numpy.random.randint().