PYTHON Tutorial

Basic Plotting Guide

Plotting Lines

  • Determine the x and y values for the line.
  • Use the plt.plot() function to plot the line, specifying the x and y values.

Plotting Points

  • Gather the x and y coordinates of the points.
  • Use the plt.scatter() function to plot the points, specifying the x and y coordinates.

Basic Plot Customization

  • Set the title, labels, and legend using the corresponding functions: plt.title(), plt.xlabel(), plt.ylabel(), and plt.legend().
  • Adjust the plot size and aspect ratio using plt.figure() and plt.gca().
  • Change the line color, width, and marker shape using the color, linewidth, and marker arguments in the plt.plot() function.

Python Example: Creating Basic Plots with Matplotlib

import matplotlib.pyplot as plt

# Plot a line
plt.plot([1, 2, 3], [4, 5, 6])
plt.title("Line Plot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")

# Plot points
plt.scatter([1, 3, 5], [7, 9, 11])
plt.title("Scatter Plot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")

# Customize the plot
plt.figure(figsize=(5, 5))
plt.grid(True)
plt.legend(["Line", "Points"])

plt.show()