Advanced_Plot_Types_with_Matplotlib
Advanced_Plot_Types_with_Matplotlib
Example Code:
1|Page
Output
Import Libraries:
• matplotlib.pyplot: This imports the pyplot module from the
Matplotlib library in Python.
• NumPy: It is used for numerical operations and generating random
data.
Generate Random Data:
• np.random.seed(42): Sets the random seed to ensure
reproducibility of the random data.
• x = np.random.rand(50): Generates an array of 50 random values
between 0 and 1.
• y = 2 * x + 1 + 0.1 * np.random.randn(50): Creates a corresponding
array 'y' using a linear relationship with some random noise.
2|Page
Add Title and Labels:
Let us now understand what histograms are and how to make them.
# Create a histogram
plt.hist(data, bins=30, color='blue', alpha=0.7)
plt.title('Histogram Example')
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.show()
3|Page
Output
Import Libraries:
• matplotlib.pyplot: This imports the pyplot module from the
Matplotlib library in Python.
• NumPy: It is used for numerical operations and generating
random data.
Create a Histogram:
plt.hist(data, bins=30, color='blue', alpha=0.7)
4|Page
Add Title and Labels:
plt.title('Histogram Example')
plt.xlabel('Values')
plt.ylabel('Frequency')
Output
5|Page
The Python code can be explained as follows:
Generate Example Data:
The code generates a random dataset of 1000 points drawn from a
standard normal distribution (mean = 0, standard deviation = 1).
Histogram:
This part manually adds a kernel density estimate (KDE) to the plot. It
calculates a Gaussian KDE using a set of points (x) and then plots the
KDE on top of the histogram.
These lines add a title and axis labels to the plot for better understanding.
6|Page
4. Box Plots: A box plot (also known as a whisker plot) is a graphical
representation of the distribution of a dataset. It provides a summary of
the central tendency, spread, and shape of the distribution. Box plots are
particularly useful for comparing distributions across different
categories or groups.
a. Key Features:
• Box: It depicts the interquartile range (IQR), the span from the
first quartile (Q1) to the third quartile (Q3), showcasing the middle
50% of the data.
• Whiskers: They stretch from the box to the minimum and
maximum values within a specified range, typically 1.5 times the
interquartile range. Data beyond the whiskers are outliers, often
shown individually.
• Median Line: Inside the box, a line denotes the dataset's median.
• Outliers: Data points beyond the whiskers are outliers and are
usually plotted separately.
b. Constructing Box Plots: Box plots are excellent for visualizing the
distribution of a dataset and identifying outliers. Here's an example:
import numpy as np
import matplotlib.pyplot as plt
7|Page
plt.title('Density Plot of the Data')
plt.xlabel('X-axis label')
plt.ylabel('Density')
Output:
8|Page