Matplotlib
Matplotlib is a powerful library for creating static, animated, and interactive
visualizations in Python. It's particularly useful for visualizing data and results in
machine learning (ML) and deep learning. Here’s a complete guide to some of the
most useful plotting functions in Matplotlib:
1. Basic Plotting Functions
[Link]() : Creates line plots.
pythonCopy code
import [Link] as plt
[Link](x, y, label='Line Plot')
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Line Plot')
[Link]()
[Link]()
[Link]() : Creates scatter plots.
pythonCopy code
[Link](x, y, c='blue', label='Scatter Plot')
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Scatter Plot')
[Link]()
[Link]()
[Link]() : Creates bar plots.
Matplotlib 1
pythonCopy code
[Link](categories, values, color='orange')
[Link]('Categories')
[Link]('Values')
[Link]('Bar Plot')
[Link]()
[Link]() : Creates histograms.
pythonCopy code
[Link](data, bins=30, color='green', alpha=0.7)
[Link]('Value')
[Link]('Frequency')
[Link]('Histogram')
[Link]()
[Link]() : Creates box plots.
pythonCopy code
[Link](data)
[Link]('Values')
[Link]('Box Plot')
[Link]()
2. Subplots and Layouts
[Link]() : Creates a figure and a set of subplots.
pythonCopy code
fig, (ax1, ax2) = [Link](1, 2, figsize=(10, 5))
[Link](x, y)
ax1.set_title('Plot 1')
Matplotlib 2
[Link](x, y)
ax2.set_title('Plot 2')
[Link]()
3. Advanced Plotting Functions
[Link]() and [Link]() : Create contour plots.
pythonCopy code
X, Y = [Link](x, y)
Z = [Link]([Link](X**2 + Y**2))
[Link](X, Y, Z)
[Link]('Contour Plot')
[Link]()
[Link]() : Displays images or heatmaps.
pythonCopy code
[Link](data, cmap='hot', interpolation='nearest')
[Link]('Heatmap')
[Link]()
[Link]()
4. Plotting for Machine Learning and Deep Learning
Confusion Matrix:
pythonCopy code
from [Link] import ConfusionMatrixDisplay
cm = confusion_matrix(y_true, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display
_labels=labels)
[Link](cmap='Blues')
Matplotlib 3
[Link]()
ROC Curve:
pythonCopy code
from [Link] import roc_curve, auc
fpr, tpr, _ = roc_curve(y_true, y_scores)
[Link](fpr, tpr, label='ROC curve (area = %0.2f)' % auc
(fpr, tpr))
[Link]('False Positive Rate')
[Link]('True Positive Rate')
[Link]('Receiver Operating Characteristic')
[Link](loc='lower right')
[Link]()
Learning Curves:
pythonCopy code
[Link](train_sizes, train_scores_mean, label='Training s
core')
[Link](train_sizes, test_scores_mean, label='Cross-valid
ation score')
[Link]('Training examples')
[Link]('Score')
[Link]('Learning Curve')
[Link]()
[Link]()
Feature Importances:
pythonCopy code
importances = model.feature_importances_
Matplotlib 4
[Link](range(len(importances)), importances)
[Link]('Feature Index')
[Link]('Importance')
[Link]('Feature Importances')
[Link]()
5. Customizing Plots
Adding Titles, Labels, and Legends:
pythonCopy code
[Link]('Title')
[Link]('X-axis Label')
[Link]('Y-axis Label')
[Link](['Label'])
Setting Limits and Ticks:
pythonCopy code
[Link](0, 10)
[Link](0, 100)
[Link]([0, 2, 4, 6, 8, 10])
[Link]([0, 20, 40, 60, 80, 100])
Saving Figures:
pythonCopy code
[Link]('[Link]', dpi=300, bbox_inches='tight')
6. Interactive Plots
Using %matplotlib notebook for interactive plots in Jupyter Notebooks:
Matplotlib 5
pythonCopy code
%matplotlib notebook
Matplotlib 6