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
  1. Plot(x, y): A fundamental method to create a basic 2D line plot where x represents the horizontal axis and y represents the vertical axis.

  2. Scatter(x, y): Useful for showcasing the relationship between two variables using scatter points, helping to reveal correlations or distributions in the data.

  3. Bar(x, height): Represents categorical data using vertical or horizontal bars, making it easy to compare different groups or categories visually.

  4. Stem(x, y): A plot that displays points connected to a baseline by vertical lines, catching attention in cases where precise values are essential.

  5. Fill_between(x, y1, y2): This function fills the area between two curves, useful for comparing two related datasets visually and highlighting the differences.

  6. Stackplot(x, y): Visualizes multiple datasets stacked on the same axis, which can be helpful in showing the composition over time or categories.

  7. 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
  1. 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.

  2. 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 medianprops for enhanced visualization.

  3. 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.

  4. Violinplot(D): Combines the box plot and a density trace to provide richer information about the distribution’s distribution shape.

  5. Eventplot(D): Visualizes individual events over time, useful in time-series data to pinpoint occurrences or trends.

  6. 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.

  7. 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.

  8. Pie(x): Provides a pie chart representation, helpful for showing proportions or percentages of a whole.

  9. 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
  1. Imshow(Z): Displays image data effectively, particularly for 2D arrays, allowing for the representation of matrix-like data visually.

  2. 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.

  3. Contour(X, Y, Z): Plots contour lines indicating levels of a Z variable over the X and Y grids, useful for visualizing topographical data.

  4. Contourf(X, Y, Z): Similar to Contour, this displays filled contour plots, making it easier to visualize gradients and transitions in data.

  5. Barbs(X, Y, U, V): Represents vector fields, where U and V denote the vector components showing wind direction or fluid flow.

  6. Streamplot(X, Y, U, V): Visualizes vector fields as flowing streamlines, representing the direction and speed of a flow in a clear manner.

  7. 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
  1. tricontour(x, y, z): Generates contour plots specifically for irregular grids, ensuring that analysis is accurate despite the non-uniformity of data points.

  2. tricontourf(X, Y, Z): Creates filled contour plots suitable for irregular grids, enhancing data representation without compromising on information precision.

  3. tripcolor(X, Y, Z): Offers a color-filled plot for irregular grid triplets, visually encoding data in a format that’s more informative.

  4. 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
  1. bar3d(x, y, z, dx, dy, dz): Generates three-dimensional bar graphs, allowing for the visualization of volumetric data in a spatial context.

  2. plot(xs, ys, zs): Enables 3D line plotting, which is essential in representing relationships and changes across three dimensions.

  3. quiver(X, Y, Z, U, V, W): Shows 3D vector fields providing insights into the behavior of flows in three-dimensional space.

  4. scatter(X, Y, Z): A 3D scatter plot that visualizes points in a three-dimensional space, useful for identifying clusters and patterns in data.

  5. Stem(x, y, z): Similar to the 2D version but for 3D data, visualizing points connected to a baseline, enhancing clarity in complex datasets.

  6. 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.

  7. Plot_trisurf(x, y, z): Creates a triangulated surface plot for 3D data, especially effective when dealing with scattered data points.

  8. 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.

  9. 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
  1. 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()
  1. 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()
  1. 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()
  1. 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.