Skip to content

Instantly share code, notes, and snippets.

@Costava
Last active October 1, 2016 19:03
Show Gist options
  • Save Costava/766f732012d826c8fd40 to your computer and use it in GitHub Desktop.
Save Costava/766f732012d826c8fd40 to your computer and use it in GitHub Desktop.
MATLAB graphing basics. Plot points and graph equations on the same graph. Set specific ranges of axes. Specify the number of decimal places to show for ticks on axes. Specify font and font size.
% Graph data points along with prediction and fit equations
clf;% Clear the graph
% Define data points
x = [0.25, 0.50, 0.75, 1.00, 1.25];
y = [1.23, 0.92, 0.76, 0.68, 0.61];
% Mark data points on graph as blue squares
% Last two arguments are optional (Default is blue circles)
scatter(x, y, 'blue', 's');
hold on;% Keep working on same graph
% Without `hold on`, new commands may clear graph
% before executing
% Graph prediction curve as red dashed line
pred = ezplot('(1.95 * x + 0.05)^(-1/2)');
pred.Color = 'red';
pred.LineStyle = '--';
% Specifying red and dashed is not required
% (Default is solid colored line)
% Graph fit curve as green solid line
fit = ezplot('(2.05 * x + 0.15)^(-1/2)');
fit.Color = 'green';
% Fit curve will be a solid line by default
% If you want specific ranges of axes:
% 0 <= x <= 1.6
% 0.25 <= y <= 1.5
axis([0 1.6 0.25 1.5]);
% Label axes and title graph
xlabel('Vehicle mass (kg)');
ylabel('Launch velocity (m/s)');
title('Launch Velocity vs. Vehicle Mass');
% If you want specific number of decimal places
% shown on axis ticks:
% Show 3 decimal places on x axis
set(gca,'xticklabel',num2str(get(gca,'xtick')','%.3f'));
% Show 2 decimal places on y axis
set(gca,'yticklabel',num2str(get(gca,'ytick')','%.2f'));
% Set font size and font (optional)
set(gca, 'FontSize', 20);
set(gca, 'FontName', 'Times New Roman');
hold off;% Done working on graph
% Further graphing commands may clear graph
% before executing
% Done!
@Costava
Copy link
Author

Costava commented Nov 6, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment