Skip to content

Instantly share code, notes, and snippets.

@mttmantovani
Last active February 11, 2019 09:18
Show Gist options
  • Save mttmantovani/30d448a9b2f48eecde40813ba5052396 to your computer and use it in GitHub Desktop.
Save mttmantovani/30d448a9b2f48eecde40813ba5052396 to your computer and use it in GitHub Desktop.
2dplot example - MATLAB
% 2D PLOT EXAMPLE
% Make grid and data
x = linspace(-5, 5, 151);
y = linspace(-3, 3, 251);
[X, Y] = meshgrid(x, y);
Z = sin(X.*Y).^2 - cos(X.*Y).^2 - X - Y;
% Save the data in a readable format to use later
coords = [X(:), Y(:)];
data = [coords, Z(:)];
dlmwrite('example.dat', data, 'precision', 5);
% Plotting
% METHOD 1
figure(1)
P1 = pcolor(x, y, Z);
box on
shading interp
set(P1,'edgecolor','none');
colorbar
% METHOD 2
figure(2)
P2 = surf(x, y, Z);
shading interp
view(0,90);
colorbar
% If you want to load data in format X,Y,Z from somewhere and redo the plot
% (data mast be sampled on a grid)
loaded_data = importdata('example.dat', ',', 0);
Xl = unique(loaded_data(:,1));
Yl = unique(loaded_data(:,2));
Zl = reshape(loaded_data(:,3), [length(Yl), length(Xl)]);
figure(3)
P3 = pcolor(Xl, Yl, Zl);
box on
shading interp
set(P3,'edgecolor','none');
colorbar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment