2D Plots
Explore various 2D plots and subplots in MATLAB and Python.
A 2D plot, also known as a 2D plot, is a graphical representation of data in which the x-axis and y-axis represent two different variables. The data points are plotted on the graph according to their x and y values, visually representing the relationship between them. 2D plots can be used to display a wide range of information, including:
Mathematical functions
Scientific data
Financial trends
They are commonly used in fields such as statistics, engineering, and physics to analyze and understand complex data sets.
We are going to discuss the most common types of 2D plots in both environments.
2D line plot
A 2D line plot is a graph that displays data as a series of points connected by lines. The x-axis and y-axis represent two different variables, and the data points are plotted on the graph according to their x and y values. Line plots are commonly used to display trends over time or to show the relationship between two variables.
For example, in MATLAB, we use plot
command to plot a 2D line plot as shown below.
f = figure ();plot([1,4,9,16,25])title('My First Plot')xlabel('Time')ylabel('Value')
Line 1: We initialize a new figure object
f
usingfigure()
.Line 2: We give an array as input to
plot()
to draw a 2D graph.Line 3: We set a figure title using
title()
function.Lines 4–5: We set labels of the x-axis and y-axis using
xlabel()
andylabel()
.
In Python, we use pyplot
from the mathplot
module. The pyplot
module provides an interface for creating a variety of different types of plots and charts. It provides a simple and convenient way to create plots and charts in Python by providing a set of functions that mimic the behavior of MATLAB’s plotting functions.
In the code below, we plot the same graph as we did above:
import matplotlib.pyplot as pltplt.figure(dpi=300)plt.plot([1,4,9,16,25])plt.title('My First Plot')plt.xlabel('Time')plt.ylabel('Value')
Line 1: We import
pyplot
frommatplotlib
and set its object toplt
.Line 3: We create a new figure with
dpi
value of300
.Line 4: We give a list as input to
plot()
function to draw a 2D graph.Line 5: We set a figure title using
title()
function.Lines 6–7: We set labels of the x-axis and y-axis using
xlabel()
andylabel()
.
2D scatter plot
A 2D scatter plot is a graph used to display data as a series of individual points without lines connecting them. Scatter plots are commonly used to display the relationship between two variables and can be used to identify patterns or outliers in the data.
In MATLAB, we use plot
command with markers for scatter plots. In ...
Get hands-on with 1400+ tech skills courses.