Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@karinjensen
Last active June 15, 2022 13:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karinjensen/cb1d9197318bb94ed67640c3bb9b2719 to your computer and use it in GitHub Desktop.
Save karinjensen/cb1d9197318bb94ed67640c3bb9b2719 to your computer and use it in GitHub Desktop.
Matlab plotting examples
clear all; close all;
%% Sample Data
x = [0 1 2 3 4 5 6];
y1 = [0 1 2 3 4 5 6];
y2 = [0 1 4 9 16 25 36];
%% Simple scatter plot
figure(1)
plot(x,y1,'k*',x,y2,'k^')
xlabel('X-axis Title (units)','FontSize',18); ylabel('Y-axis Title (units)','FontSize',18)
legend('Series 1 Label','Series 2 Label','Location','NorthWest')
axis([0 7 0 40])
%% Bar graph
x = [1 2 3];
y1 = [1 2 3 4 5 6];
y2 = [1 4 9 16 25 36];
y3 = [1 1 1 1 1 1];
figure(2)
bar(x,[y1;y2;y3])
xlabel('X-axis Title (units)','FontSize',18); ylabel('Y-axis Title (units)','FontSize',18)
legend('Series 1','Series 2','Series 3','Series 4','Series 5', 'Series 6','Location','NorthWest')
colormap(gray)
%% Error Bars
%Equal Error Bars
x = [0 1 2 3 4 5 6];
y1 = [0 1 4 9 16 25 36];
E1 = [3 3 3 3 3 3 3];
figure(3)
errorbar(x,y1,E1,'k^')
xlabel('X-axis Title (units)','FontSize',18); ylabel('Y-axis Title (units)','FontSize',18)
legend('Series 1 Label','Location','NorthWest')
%Unequal Error Bars
x = [0 1 2 3 4 5 6];
y1 = [0 2 4 6 8 10 12];
L =[1 1 1 1 1 1 1];
U = [2 2 2 2 2 2 2];
figure(4)
errorbar(x,y1,L,U,'k^')
xlabel('X-axis Title (units)','FontSize',18); ylabel('Y-axis Title (units)','FontSize',18)
legend('Series 1 Label','Location','NorthWest')
axis([-1 7 0 15])
%% Simple Linear Regression
y3 = [0 1.5 2.3 3.8 4.2 4.8 6.1];
figure(5)
plot(x,y3,'k.')
xlabel('X-axis Title (units)','FontSize',18); ylabel('Y-axis Title (units)','FontSize',18)
p=polyfit(x,y3,1);
hline=refline(p);
hline.Color='k';
%values of p give m and b values for regression
%% Bar graph with error bars
x = [1 2 3 4 5 6];
y1 = [1 2 3 4 5 6];
e = [0.1 0.08 0.05 0.04 0.11 0.7];
color_gray = [192 192 192]./255;
figure(6)
box on
hold on
bar(x,y1,'FaceColor', color_gray)
errorbar(x,y1,e, '.k')
xlabel('X-axis Title (units)','FontSize',18); ylabel('Y-axis Title (units)','FontSize',18)
%Add names to bars instead of numbers
names = {'A' 'B' 'C' 'D' 'E' 'F'};
set(gca, 'XTick', [1 2 3 4 5 6],'xticklabel', names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment