Overlaying contour lines on top of contourf plot - matlab

I am trying to plot contour lines of one data set on top of filled contours of another data set. Plotted individually they both look correct but when I combine them in the usual way the plots don't look right:
clc; clear all; close all;
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X1,Y1] = meshgrid(x,y);
Z1 = sin(X1)+cos(Y1);
[X2,Y2] = meshgrid(x,y);
Z2 = 1000*(sin(1.2*X2)+2*cos(Y2));
figure;
contourf(X1,Y1,Z1);
shading flat;
figure;
contour(X2,Y2,Z2,'k');
figure;
contourf(X1,Y1,Z1);
shading flat;
hold on;
contour(X2,Y2,Z2,'k');

To fix this you have to use caxis to set the limits for the contourf plot:
clc; clear all; close all;
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X1,Y1] = meshgrid(x,y);
Z1 = sin(X1)+cos(Y1);
[X2,Y2] = meshgrid(x,y);
Z2 = 1000*(sin(1.2*X2)+2*cos(Y2));
figure;
contourf(X1,Y1,Z1);
shading flat;
caxis([min(min(Z1)) max(max(Z1))]);
hold on;
contour(X2,Y2,Z2,'k');
You can replace min(min(Z1)) and max(max(Z1)) with the upper and lower limits that you want. This results in this plot:

Related

Disable axes scale in Matlab

When I use ginput to mark tqo points it is followed by updating the axes scales. How can I stop the "auto-scaling" of the axes in Matlab?
Code:
clc;
clear all;
close all;
grid on;
message = sprintf('Select two points for a line.');
uiwait(helpdlg(message));
[x, y] = ginput(2);
Xorigin1 = x(1);
Xorigin2 = x(2);
Yorigin1 = y(1);
Yorigin2 = y(2);
plot([Xorigin1,Xorigin2],[Yorigin1,Yorigin2], 'b-', 'LineWidth',2);
hold on;
plot([Xorigin1,Xorigin2],[Yorigin1,Yorigin2], 'r+', 'LineWidth',2,'MarkerSize', 12);
Both the x- and y-axis are by default set to automically rescale their limits whenever data in the figure is changed.
You can overwrite this using
xlim manual
ylim manual

How to smooth the edges in my contour plot corresponding to nan

This is the link to the dataset. I have this contour plot which has a bit rough edges. My question is, how can I smooth these edges these edges correspond to Nan. I filled in the Z matrix with Nan so as to remove unwanted values.
I also wanted to ask that why shading flat and interp is not working on this contour.
I have set shading to flat and in Matlab2013b I get proper flat figure but in Matlab 2014b and 2015b I am getting this figure.
MATLAB 2015b:
MATLAB 2013b
How can I obtain perfectly meshed plot in Matlab 2015b, I checked for shading options in the documentation and there are only 3 faceted, interp and flat.
shading flat works in 2013b but not in subsequent versions. Can someone tell me why is it so?
This is the sample code which I am using right now:
clear all; close all; clc;
load temperature.txt;
time = temperature(:,1); % This column contains the time
x = temperature(:,2); % This column contains the x values.
temperature_system = temperature(:,3); % This column contains the temperatures.
% Rejecting the outliers
pos = (temperature_system > prctile(temperature_system,97));
time(pos) = [];
x(pos) = [];
temperature_system(pos) = [];
X1 = [time x];
F = scatteredInterpolant(X1,temperature_system);
x1 = linspace(min(x),max(x),100);
x2 = linspace(min(time),max(time),100);
[X,Y] = meshgrid(x2,x1);
Z = F(X,Y);
% Is the data below the criteria for all points in space at a specific time
emptyTime = all(Z<10,1);
emptySpace = all(Z<10,2);
[emptyTime, emptySpace] = meshgrid(emptyTime, emptySpace);
Z(emptyTime | emptySpace) = nan;
% Replacing the remaining zeros with nan
pos = find(Z<1);
Z(pos) = nan;
f1 = figure(1);
%set(f1,'renderer','zbuffer');
%surf(X,Y,Z);
[C,h] = contourf(X,Y,Z, 'Linestyle', 'none');
shading flat;
colormap(jet);
q = colorbar;
set(q,'direction','reverse');
q.Label.String = 'Temperature';
xlabel('Time (ps)','FontSize', 16, 'FontWeight', 'bold',...
'FontName', 'Helvetica', 'Color', 'Black');
ylabel('Length of box (A)','FontSize', 16, 'FontWeight', 'bold',...
'FontName', 'Helvetica', 'Color', 'Black');
set(gca,'LineWidth',3,'TickLength',[0.02 0.02]);
set(gca,'XMinorTick','on');
set(gca,'YMinorTick','on','XTicksBetween', 5);
set(gca,'FontSize',12,'FontName','Helvetica');
It's difficult to test the issue without having your data. I got rid of the lines by means of the LineStyle property:
Code:
Z = peaks(20);
subplot(2,1,1);
contourf(Z,10);
colorbar;
subplot(2,1,2);
contourf(Z,10, 'LineStyle', 'none');
colorbar;

How can I make a perfectly square plot with a colorbar in MATLAB?

I am trying to create a contour plot (with colorbar) which has the contour plot perfectly square, for aesthetic/data interpretation reasons.
Example code:
x=-10:10;
y=-10:10;
[X Y] = meshgrid(x,y);
f = figure('Visible','off');
contour(X,Y,X.*Y,'Fill','on');
colorbar
x_size = 10;
y_size = 10;
set(f, 'PaperUnits', 'inches','PaperPosition',[0 0 x_size y_size]);
print(f, '-depsc2','notsquare.eps);
Is there a way to control the dimensions of just the contour plot region such that it will be perfectly square?
This works for me (adding axis equal):
x=-10:10;
y=-10:10;
[X Y] = meshgrid(x,y);
f = figure('Visible','off');
contour(X,Y,X.*Y,'Fill','on');
colorbar, axis equal
x_size = 10;
y_size = 10;
set(f, 'PaperUnits', 'inches','PaperPosition',[0 0 x_size y_size]);
print(f, '-depsc2','square.eps');

3D body plot in matlab ( volume visualization )

I have little or no experience with volumetric data in MATLAB,
I need to complete next task:
I have 3 vectors ( rows ):
x_ = vector(1:smpl:max_row,1);
y_ = vector(1:smpl:max_row,2);
z_ = vector(1:smpl:max_row,3);
that are samples from large 3 columns array vector with height max_row.
x_ , y_ , z_ are points of 3D figure - surface points of the figure ( volume ). They represent 3D body that should be drawn in matlab.
I created linear grid:
%linear grid
a = -1.1:step:(-1.1+step*(length(x_)-1));
b = -1.1:step:(-1.1+step*(length(y_)-1));
c = -1.1:step:(-1.1+step*(length(z_)-1));
[x,y,z] = meshgrid(-1.1:step:(-1.1+step*(length(x_)-1)));
and also I create array v length(x_)*length(x_)*length(x_) that contains '1' in cells that are of 3D body representation function points and '0' another.
I tryied to make interpolation:
vi = interp3(x,y,z,v,x,y,z,'nearest');
but then vi = v that I've already created.
Now I need to plot the v array on 3D and form 3D body like in
http://www.mathworks.com/help/techdoc/ref/isonormals.html
for example.
I make that next way:
%plotting:
figure
p = patch(isosurface(x,y,z,v,1e-5,'verbose'),'FaceColor','green','EdgeColor','none');
grid on;
isonormals(v,p);
daspect([1 1 1])
view(3);
axis tight;
camproj perspective;
camlight
lighting gouraud
colormap(hsv)
but I get then only small rectangles in place of function '1' that are not connected like in picture that is attached.
I expect solid body enclosed by that points to be plotted.
Does anybody know what is the problem , how to draw 3D body from the x,y,z,v arrays ?
Thanks in advance.
image:
http://imgur.com/Ulegj
Try this, which will be a nice plot (it interpolates a bit though):
x = vector(1:smpl:max_row,1);
y = vector(1:smpl:max_row,2);
z = vector(1:smpl:max_row,3);
% Settings
displaySurface = 1;
displayPoints = 0;
xres = 800; % Resolution, the higher, the smoother
yres = 800;
cm = 'default'; % Colormap
% Axes Limits
xmin = min(x);
ymin = min(y);
xmax = max(x);
ymax = max(y);
xi = linspace(xmin, xmax, xres);
yi = linspace(ymin, ymax, yres);
% Figure
myfig = figure('Position', [200 200 800 600]);
rotate3d off
[XI, YI] = meshgrid(xi, yi);
ZI = griddata(x, y, z, XI, YI, 'cubic');
mesh(XI,YI,ZI);
colormap(cm)
if(displaySurface == 1)
hold on;
surf(XI, YI, ZI, 'EdgeColor', 'none');
end
hold on;
xlabel('x');
ylabel('y');
zlabel('z');
title('Title', 'FontWeight', 'bold');
xlim([xmin xmax])
ylim([ymin ymax])
grid off;
if(displayPoints == 1)
hold on
plot3(x, y, z,'marker','p','markerfacecolor','w','linestyle','none')
hidden off
end

Plot outside axis in Matlab

How to plot something outside the axis with MATLAB? I had like to plot something similar to this figure;
Thank you.
Here is one possible trick by using two axes:
%# plot data as usual
x = randn(1000,1);
[count bin] = hist(x,50);
figure, bar(bin,count,'hist')
hAx1 = gca;
%# create a second axis as copy of first (without its content),
%# reduce its size, and set limits accordingly
hAx2 = copyobj(hAx1,gcf);
set(hAx2, 'Position',get(hAx1,'Position').*[1 1 1 0.9], ...
'XLimMode','manual', 'YLimMode','manual', ...
'YLim',get(hAx1,'YLim').*[1 0.9])
delete(get(hAx2,'Children'))
%# hide first axis, and adjust Z-order
axis(hAx1,'off')
uistack(hAx1,'top')
%# add title and labels
title(hAx2,'Title')
xlabel(hAx2, 'Frequency'), ylabel(hAx2, 'Mag')
and here is the plot before and after:
You can display one axis with the scale you want, then plot your data on another axis which is invisible and large enough to hold the data you need:
f = figure;
% some fake data
x = 0:20;
y = 23-x;
a_max = 20;
b_max = 23;
a_height = .7;
%% axes you'll see
a = axes('Position', [.1 .1 .8 a_height]);
xlim([0 20]);
ylim([0 20]);
%% axes you'll use
scale = b_max/a_max;
a2 = axes('Position', [.1 .1 .8 scale*a_height]);
p = plot(x, y);
xlim([0 20]);
ylim([0 b_max]);
set(a2, 'Color', 'none', 'Visible', 'off');
I had similar problem and I've solved it thanks to this answer. In case of bar series the code is as follows:
[a,b] = hist(randn(1000,1)); % generate random data and histogram
h = bar(b,a); % plot bar series
ylim([0 70]) % set limits
set(get(h,'children'),'clipping','off')% turn off clippings