7.6. Python Plotting Guide#
7.6.1. Popular Matplotlib Plots#
Plot Type |
Description |
---|---|
plot |
Plots a line graph. It is commonly used for visualizing continuous data, like time series or continuous functions. |
scatter |
Creates a scatter plot, used for visualizing the relationship between two numerical variables. Each data point is represented by a marker on the plot. |
bar |
Plots vertical bars, representing the values of different categories or groups. Useful for comparing discrete data. |
barh |
Similar to bar, but plots horizontal bars instead. |
hist |
Creates a histogram, which shows the distribution of a continuous variable by dividing it into bins and counting the number of occurrences in each bin. |
pie |
Plots a pie chart, used to show the composition of a whole in terms of parts. Each segment represents a proportion of the whole. |
imshow |
Displays an image on the plot, useful for visualizing 2D arrays or images. |
contour |
Plots contour lines, connecting points of equal value in a 2D array. Useful for visualizing functions of two variables. |
contourf |
Similar to contour, but fills the area between contour lines with colors. |
quiver |
Displays arrows representing vectors at specified points. |
streamplot |
Plots streamlines of a vector field. |
boxplot |
Creates a box-and-whisker plot, which provides a compact way to represent the distribution of data. |
violinplot |
Plots violin plots, a combination of a box plot and a kernel density plot. |
stackplot |
Plots stacked areas, useful for showing the contribution of each part to the whole. |
errorbar |
Plots error bars to represent uncertainty or variability in data. |
fill_between |
Fills the area between two curves or between a curve and a specified value. |
fill_betweenx |
Similar to fill_between, but for horizontal filling. |
fill |
Fills the area enclosed by a polygon defined by x and y coordinates. |
stem |
Creates a stem plot, used for visualizing discrete data. |
plot_date |
Similar to plot, but specialized for plotting dates on the x-axis. |
hexbin |
Plots a hexagonal binning plot, useful for visualizing the distribution of large datasets. |
text |
Adds text to the plot at specified coordinates. |
annotate |
Adds an annotation with an arrow connecting it to a specific data point on the plot. |
arrow |
Adds an arrow to the plot at specified coordinates. |
barbs |
Plots barbs (arrows) to represent wind speed and direction. |
Please note that some of these plot functions have additional parameters for customization, such as colors, markers, line styles, etc. The exact usage and customization options can be found in the official matplotlib
documentation.
This is just a simple example, and Seaborn offers many more options and customizations for different types of plots. You can refer to the official Seaborn documentation for more details and examples: https://seaborn.pydata.org/.
A list of commonly used matplotlib plot functions along with their object-oriented equivalents.
MATLAB-Style |
Object-Oriented |
Description |
---|---|---|
plt.plot() |
ax.plot() |
Plot lines and/or markers. |
plt.scatter() |
ax.scatter() |
Create a scatter plot. |
plt.bar() |
ax.bar() |
Make a bar plot. |
plt.barh() |
ax.barh() |
Create a horizontal bar plot. |
plt.hist() |
ax.hist() |
Plot a histogram. |
plt.pie() |
ax.pie() |
Create a pie chart. |
plt.imshow() |
ax.imshow() |
Display an image. |
plt.contour() |
ax.contour() |
Plot contour lines. |
plt.contourf() |
ax.contourf() |
Plot filled contour. |
plt.quiver() |
ax.quiver() |
Plot a 2D field of arrows. |
plt.streamplot() |
ax.streamplot() |
Plot 2D streamlines. |
plt.boxplot() |
ax.boxplot() |
Make a box and whisker plot. |
plt.violinplot() |
ax.violinplot() |
Create a violin plot. |
plt.stackplot() |
ax.stackplot() |
Draw a stacked area plot. |
plt.errorbar() |
ax.errorbar() Plot error b |
ars. |
plt.fill_between() |
ax.fill_between() |
Fill between y-values and 0. |
plt.fill_betweenx() |
ax.fill_betweenx() |
Fill between x-values and 0. |
plt.fill() |
ax.fill() |
Make filled polygons. |
plt.plot_date() |
ax.plot_date() |
Plot data that contains dates. |
plt.hexbin() |
ax.hexbin() |
Make a hexagonal binning plot. |
plt.text() |
ax.text() |
Add text to the axes. |
plt.annotate() |
ax.annotate() |
Annotate a point on the plot. |
plt.arrow() |
ax.arrow() |
Add an arrow to the axes. |
plt.barbs() |
ax.barbs() |
Plot 2D field of arrows as barbs. |
plt.stem() |
ax.stem() |
Create a stem plot. |
Please note that this is not an exhaustive list, but it covers many of the commonly used plot functions in matplotlib. The object-oriented equivalents typically follow the pattern of ax.plot(), ax.scatter(), ax.bar(), etc., where ax is the Axes object obtained using fig, ax = plt.subplots(). The object-oriented interface allows for more control and customization of the plot elements.
7.6.2. Popular Seaborn Plots#
A list of commonly used seaborn plot functions along with their brief descriptions.
Seaborn Plot |
Description |
---|---|
Scatter Plot |
Visualizes the relationship between two continuous variables. |
Line Plot |
Displays trends and patterns over continuous data points. |
Bar Plot |
Compares categorical data or discrete variables. |
Histogram |
Shows the distribution of a continuous variable in bins. |
Box Plot |
Visualizes data distribution and identifies outliers. |
Violin Plot |
Similar to box plot, shows data density along with distribution. |
Heatmap |
Displays the correlation between variables in a matrix format. |
Pair Plot |
Visualizes pairwise relationships in a dataset. |
Joint Plot |
Combines scatter plot and histograms for two variables. |
KDE (Kernel Density Estimation) Plot |
Estimates probability density function of a variable. |
Swarm Plot |
Displays individual data points along a categorical axis. |
7.6.3. Python Plotting Guide#
Python’s popular plotting libraries, Matplotlib and Seaborn, offer a wide range of plot types suitable for various data visualization scenarios. Below is a detailed list of common plot types and the scenarios in which they are typically used {cite:p}`Waskom2021, MatplotlibDocumentation}:
Line Plot: Used to visualize trends and patterns over continuous data points, such as time series data, stock prices, or any data with a continuous x-axis. It helps show the relationship between two variables over time or a continuous sequence.
Scatter Plot: Suitable for visualizing the relationship between two continuous variables. Used to identify correlations or clusters in the data. It helps to understand the distribution and dispersion of data points.
Bar Plot: Used to compare categorical data or discrete variables. Suitable for showing the distribution of data across different categories. It allows easy comparison between different categories.
Histogram: Used to display the distribution of a continuous variable by dividing it into bins and showing the frequency or count in each bin. It helps to understand the underlying distribution of data.
Pie Chart: Used to represent parts of a whole, where each segment of the pie represents a percentage of the total. Ideal for showing the proportion of different categories in a dataset.
Box Plot (Box-and-Whisker Plot): Used to visualize the distribution of data and identify outliers. Suitable for comparing multiple datasets or groups. It provides a quick summary of the data’s central tendency and variability.
Violin Plot: Similar to the box plot, but it also shows the probability density of the data at different values, making it ideal for comparing distributions. Useful when comparing multiple groups’ distributions.
Heatmap: Used to display the correlation between variables in a matrix format, with colors representing the magnitude of the correlation. Useful for visualizing relationships between multiple variables simultaneously.
Pair Plot: Specifically used in Seaborn. Visualizes the pairwise relationship between multiple variables in a dataset, showing scatter plots and histograms. Helps identify correlations between pairs of variables.
Joint Plot: Also specific to Seaborn. Combines scatter plots and histograms to show the relationship between two variables and their univariate distributions. Useful for understanding the relationship and distribution of two variables simultaneously.
Contour Plot: Used to visualize 3D data on a 2D plane by showing lines of constant values (contours) for a third variable. Often used in scientific and engineering data to represent three-dimensional data in a two-dimensional space.
3D Plot: Used to visualize three-dimensional data, often for showing complex relationships among three variables. Commonly used in scientific visualization and surface plotting.
Area Plot: Similar to a line plot, but the area between the line and the x-axis is filled. Useful for visualizing cumulative data or stacked trends. Suitable for showing the composition of data over time or different categories.
Barh Plot (Horizontal Bar Plot): Similar to a bar plot, but with the bars displayed horizontally. Useful when the category names are lengthy or when comparing data across the y-axis. It offers an alternative view for comparing categories.
Swarm Plot: Specifically used in Seaborn. Displays individual data points along a categorical axis, helping to visualize the distribution of data. Suitable for small datasets to show individual data points’ positions and density.
KDE (Kernel Density Estimation) Plot: Used to estimate the probability density function of a continuous variable. Often used in combination with other plots like the histogram or violin plot. Helps understand the data’s underlying continuous distribution.
These are just some of the common plot types used in data visualization with Matplotlib and Seaborn. Depending on your data and specific goals, you might choose one or a combination of these plots to effectively convey insights from your data. Always consider the nature of your data and the message you want to communicate when selecting the appropriate plot type.