Lecture 9 - Plot Types in Python
Scientific Data Visualization in Python
Overview
Course Code: CS-657
Topic: Scientific Data Visualization
Instructor: Dr. Rohit P. Singh
Institution: University of Wisconsin, Milwaukee
Date: February 19, 2025
Plot Types
Pairwise Data
Plot(x, y): A fundamental method to create a basic 2D line plot where
xrepresents the horizontal axis andyrepresents the vertical axis.Scatter(x, y): Useful for showcasing the relationship between two variables using scatter points, helping to reveal correlations or distributions in the data.
Bar(x, height): Represents categorical data using vertical or horizontal bars, making it easy to compare different groups or categories visually.
Stem(x, y): A plot that displays points connected to a baseline by vertical lines, catching attention in cases where precise values are essential.
Fill_between(x, y1, y2): This function fills the area between two curves, useful for comparing two related datasets visually and highlighting the differences.
Stackplot(x, y): Visualizes multiple datasets stacked on the same axis, which can be helpful in showing the composition over time or categories.
Stairs(values): Creates a step-like plot useful to represent data that changes at irregular intervals, often used in histograms to depict frequencies.
Statistical Distributions
Hist(x): Plots the distribution of numerical data, with multiple bins representing ranges of values. For example,
ax.hist(x, bins=8)creates a histogram that can help identify the frequency distribution of the data.Boxplot(X): A diagram that summarizes data distributions using five-number summary statistics (minimum, first quartile, median, third quartile, maximum). Boxplots are customizable using parameters like
medianpropsfor enhanced visualization.Errorbar(x, y, yerr, xerr): This function includes error bars in the plot to indicate variability or uncertainty in the data points, which is crucial in scientific visualizations.
Violinplot(D): Combines the box plot and a density trace to provide richer information about the distribution’s distribution shape.
Eventplot(D): Visualizes individual events over time, useful in time-series data to pinpoint occurrences or trends.
Hist2d(x, y): Displays a two-dimensional histogram where counts of points falling in the X and Y ranges are represented, allowing for a comprehensive view of joint distributions. For example,
ax.hist2d(x, y, bins=(np.arange(-3, 3, 0.1), np.arange(-3, 3, 0.1)))creates a 2D heatmap.Hexbin(x, y, C): A variant of the scatter plot, presenting data in hexagonal bins and coloring them based on the count of data points; effective in visualizing large datasets.
Pie(x): Provides a pie chart representation, helpful for showing proportions or percentages of a whole.
Ecdf(x): Displays the empirical cumulative distribution function, which is useful for understanding the probability of a statistic being less than or equal to a certain value.
Gridded Data
Imshow(Z): Displays image data effectively, particularly for 2D arrays, allowing for the representation of matrix-like data visually.
Pcolormesh(X, Y, Z): Creates a pseudocolor plot where color represents the value of data on a non-regular grid, effective in visualizing geographic information.
Contour(X, Y, Z): Plots contour lines indicating levels of a Z variable over the X and Y grids, useful for visualizing topographical data.
Contourf(X, Y, Z): Similar to
Contour, this displays filled contour plots, making it easier to visualize gradients and transitions in data.Barbs(X, Y, U, V): Represents vector fields, where
UandVdenote the vector components showing wind direction or fluid flow.Streamplot(X, Y, U, V): Visualizes vector fields as flowing streamlines, representing the direction and speed of a flow in a clear manner.
Quiver(x, y, C): Provides a concise visualization of vector fields using arrows, where the direction and length provide information about the vectors’ components.
Irregular Gridded Data
tricontour(x, y, z): Generates contour plots specifically for irregular grids, ensuring that analysis is accurate despite the non-uniformity of data points.
tricontourf(X, Y, Z): Creates filled contour plots suitable for irregular grids, enhancing data representation without compromising on information precision.
tripcolor(X, Y, Z): Offers a color-filled plot for irregular grid triplets, visually encoding data in a format that’s more informative.
triplot(X, Y): Displays a network of triangles connecting points in an irregular grid, often used in finite element analysis or simulations.
3D and Volumetric Data
bar3d(x, y, z, dx, dy, dz): Generates three-dimensional bar graphs, allowing for the visualization of volumetric data in a spatial context.
plot(xs, ys, zs): Enables 3D line plotting, which is essential in representing relationships and changes across three dimensions.
quiver(X, Y, Z, U, V, W): Shows 3D vector fields providing insights into the behavior of flows in three-dimensional space.
scatter(X, Y, Z): A 3D scatter plot that visualizes points in a three-dimensional space, useful for identifying clusters and patterns in data.
Stem(x, y, z): Similar to the 2D version but for 3D data, visualizing points connected to a baseline, enhancing clarity in complex datasets.
Plot_surface(X, Y, Z): Plots a 3D surface based on the provided grid, useful for surface analysis and the visual representation of three-dimensional functions.
Plot_trisurf(x, y, z): Creates a triangulated surface plot for 3D data, especially effective when dealing with scattered data points.
Voxels([x, y, z], filled): Represents volumetric data in a grid format, particularly useful in fields like medical imaging or scientific simulation where 3D data is prevalent.
Plot_wireframe(X, Y, Z): Creates wireframe representations of surfaces, ideal for showcasing surface shape without solid fill.
Summary of Important Functions and Code Snippets
Basic line plot example:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = 4 + 1 * np.sin(2 * x)
plt.plot(x, y)
plt.title('Basic Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True)
plt.show()
Scatter plot example:
x = np.random.normal(size=100)
y = np.random.normal(size=100)
plt.scatter(x, y)
plt.title('Scatter Plot Example')
plt.xlabel('X-axis Values')
plt.ylabel('Y-axis Values')
plt.grid(True)
plt.show()
Histograms and Boxplots:
data = np.random.normal(size=100)
plt.hist(data, bins=10, alpha=0.7, color='blue', edgecolor='black')
plt.boxplot(data, vert=False)
plt.title('Histogram and Boxplot')
plt.xlabel('Data Values')
plt.show()
3D Plot Example:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xs, ys, zs)
ax.set_title('3D Scatter Plot')
plt.show()
By understanding and utilizing these various plot types and their associated code snippets, one can effectively communicate data-driven insights through scientific visualizations in Python, enhancing data analysis and interpretation in scientific research.