Python 2.1.4
Python 2.1.4
Visualization with Matplotlib: Simple Line Plots, Simple Scatter Plots, Visualizing Errors, Density
and Contour Plots, Histograms, Binnings, and Density, Customizing Plot Legends, Customizing Color
bars, Multiple Subplots, Text and Annotation, Customizing Tick s, Customizing Matplotlib:
Configurations and Stylesheets, Three- Dimensional Plotting in Matplotlib, Geographic Data with
Basemap, Visualization with Seaborn
Theory:
Line plots are one of the most commonly used plots for visualizing the relationship between
two continuous variables. In Matplotlib, you can create line plots using the plot() function,
where the x-axis represents one variable and the y-axis represents the other.
Code Example:
Explanation:
Output: A simple line plot with points (1, 2), (2, 4), etc., connected by a line.
Theory:
A scatter plot is used to show the relationship between two continuous variables by plotting
individual data points on a 2D graph. In Matplotlib, you can create scatter plots using
scatter().
Code Example:
Explanation:
plt.scatter(x, y) creates a scatter plot with points at the coordinates (x, y).
The title(), xlabel(), and ylabel() functions are used to add a title and axis
labels.
Output: A scatter plot with individual points at the coordinates (1, 5), (2, 7), etc.
Theory:
Error bars represent the uncertainty in the data, showing the range within which the true
values might lie. You can add error bars to your plots in Matplotlib using the errorbar()
function.
Code Example:
Explanation:
The yerr parameter represents the vertical error for each point.
fmt='o' specifies the format of the plot (in this case, circle markers).
Output: A plot with blue points and red error bars showing uncertainty for each data point.
4. Density and Contour Plots
Theory:
Density plots visualize the distribution of data over a continuous interval. Contour plots show
the 2D density of data by drawing lines where the density is the same.
import numpy as np
import matplotlib.pyplot as plt
Explanation:
Theory:
Histograms are used to represent the distribution of a dataset. The x-axis represents data
intervals (bins), while the y-axis shows the frequency of values within each bin.
Code Example:
# Create a histogram
plt.hist(data, bins=30, density=True, alpha=0.6, color='g')
plt.title("Histogram with Density")
plt.xlabel("Data Values")
plt.ylabel("Frequency")
plt.show()
Explanation:
Theory:
Legends help identify what different plot elements represent. In Matplotlib, legends can be
customized using the legend() function.
Code Example:
# Data
x = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
y2 = [1, 3, 5, 7, 9]
# Add a legend
plt.legend(loc='best')
plt.title("Custom Legend")
plt.show()
Explanation:
Output: Two lines with a legend that labels them "Line 1" and "Line 2."
Theory:
Color bars are used to indicate the scale of values in a plot, especially for heatmaps or density
plots. You can customize the color bar to suit the plot's style.
Code Example:
import numpy as np
import matplotlib.pyplot as plt
# Create heatmap
plt.imshow(data, cmap='viridis')
Explanation:
8. Multiple Subplots
Theory:
Multiple subplots allow you to display different plots in the same figure. You can use
subplot() or subplots() to arrange multiple plots.
Code Example:
plt.tight_layout()
plt.show()
Explanation:
Theory:
You can add custom text and annotations to your plots using text() and annotate(). These
allow you to highlight specific data points or provide additional information.
Code Example:
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create plot
plt.plot(x, y)
plt.show()
Explanation:
Output: A plot with an annotation pointing to the max point (5, 10).
Theory:
Ticks on the axes can be customized in terms of their positions, labels, and appearance using
xticks() and yticks() functions.
Code Example:
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a plot
plt.plot(x, y)
plt.show()
Explanation:
xticks() customizes the x-axis ticks by changing their positions and labels.
Output: A line plot where the x-axis has custom labels A, B, C, D, and E.
Theory:
Matplotlib allows you to customize the appearance of your plots using stylesheets or by
modifying the rcParams. You can choose from predefined styles or create your own.
Code Example:
Explanation:
style.use() applies a predefined style to the plot, such as 'ggplot', which changes
the color scheme and grid.
Output: A plot with a grid and a specific color scheme from the 'ggplot' style.
Theory:
Matplotlib supports 3D plotting using the Axes3D module. You can create 3D scatter plots,
surface plots, and wireframe plots.
Code Example:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Data
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
# Create 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, cmap='viridis')
plt.show()
Explanation:
Theory:
Basemap is a Matplotlib toolkit for plotting geographic data on a map. It provides tools for
creating maps with projections, coastlines, and other geographic features.
Code Example:
# Draw coastlines
m.drawcoastlines()
plt.show()
Explanation:
Theory:
Seaborn is built on top of Matplotlib and provides a high-level interface for creating
informative and attractive statistical graphics. It integrates well with pandas DataFrames and
simplifies tasks like plotting distributions, regression lines, and categorical plots.
Code Example:
# Create a boxplot
sns.boxplot(x='species', y='sepal_length', data=data)
plt.show()
Explanation:
sns.boxplot() creates a box plot of the sepal_length for each species in the iris
dataset.
Output: A box plot showing the distribution of sepal lengths for each species in the Iris
dataset.
Questions:
2. Describe the difference between a line plot and a scatter plot. In which
scenarios would each be used?
Explain what a density plot is and how it is used to represent data distributions.
Compare it to a histogram and discuss when each visualization is useful.
4. What is the purpose of using error bars in a plot, and how can they be
implemented in Matplotlib?
5. What are histograms, and what does binning mean in the context of
histograms?
6. How do you add legends to a plot in Matplotlib, and why are they
important?