How to Plot in MATLAB?

Use `plot(x,y)` to create a 2D line plot

Define data vectors, for example `x = 0:0.1:10;`

Define corresponding values, for example `y = sin(x);`

Call `plot(x,y)`

Use `xlabel(‘X Axis’)` to label the x-axis

Use `ylabel(‘Y Axis’)` to label the y-axis

Use `title(‘My Plot’)` to add a title

Use `grid on` to show grid lines

Use `legend(‘Data 1’)` to add a legend

Use `hold on` to overlay multiple plots

Use `hold off` to stop overlaying plots

Use `figure` to open a new plot window

Use `subplot(m,n,p)` to create multiple plots in one figure

Use `scatter(x,y)` for scatter plots

Use `bar(x,y)` for bar charts

Use `histogram(data)` for histograms

Use `stem(x,y)` for discrete plots

Use `plot3(x,y,z)` for 3D line plots

Use `surf(X,Y,Z)` for surface plots

Use `mesh(X,Y,Z)` for mesh plots

Use `axis([xmin xmax ymin ymax])` to set axis limits

Use `saveas(gcf,’filename.png’)` to save the figure

Suggested for You

Trending Today