2D Plot in MATLAB

MATLAB Tutorial

📈 2D Plot in MATLAB

A 2D plot in MATLAB is used to visualize the relationship between two variables on X–Y axes.

It is one of the most common tools for data analysis, engineering, and scientific visualization.
MATLAB is developed by MathWorks.


🔹 What Is a 2D Plot?

A 2D plot displays:

  • X-axis → independent variable

  • Y-axis → dependent variable

Created mainly using the plot() function.


1️⃣ Basic 2D Plot

🔸 Syntax


🔸 Example


Output:

A graph of y = x² 📈


2️⃣ 2D Plot with Labels & Title


 

Output:
A labeled sine wave.


3️⃣ Plot Multiple Lines on Same Graph


 

Output:
Sine and cosine curves on the same plot.


4️⃣ Changing Line Style, Color & Marker


 

📌 Meaning of 'r--o':

  • r → red color

  • -- → dashed line

  • o → circle marker


5️⃣ Grid on 2D Plot


📌 Improves readability.


6️⃣ Plot Using hold on


 

📌 Plots multiple graphs without erasing previous one.


7️⃣ Axis Control


📌 Format: axis([xmin xmax ymin ymax])


8️⃣ Complete Example


 


⚠️ Important Notes

  • plot() creates a 2D line graph

  • Use .^, .*, ./ for vector math

  • Always label axes for clarity

  • Use legend() when plotting multiple lines


🎯 Interview Questions: 2D Plot in MATLAB

🔹 Q1. Which function is used to create a 2D plot?

Answer: plot()


🔹 Q2. How do you label X and Y axes?

Answer: Using xlabel() and ylabel().


🔹 Q3. How do you plot multiple lines on one graph?

Answer:
Using plot(x1,y1,x2,y2) or hold on.


🔹 Q4. What does grid on do?

Answer:
Displays grid lines on the plot.


🔹 Q5. Which operator is used for element-wise square?

Answer: .^


🔹 Q6. How do you add a legend?

Answer:
Using legend().


🔹 Q7. How do you change line color and style?

Answer:
Using format strings like 'r--o'.


Summary

  • 2D plots visualize X–Y data

  • Created using plot()

  • Supports labels, legends, grid, styles

  • Essential for data visualization

You may also like...