Fill color inside a shape on 3d plot matlab - matlab

I have created a shape in matlab which look like this
.
npts = 14;
z = linspace(-1,1,npts);
omz = sqrt(1-z.^2);
xyz=[2 3 1 0.5 2 5 10 15 20 29 32 34 10 2;1 3 8 12 20 25 27 25 23 26 26 23 5 1;z];
plot3(xyz(1,:),xyz(2,:),xyz(3,:),'ro','LineWidth',2);
box on
hold on
fnplt(cscvn(xyz(:,[1:end 1])),'r',2)
hold off
I need to fill different colors in this shape. How can I do this?

Take the output of fnplt.
change the colormap to gray.
use fill3 to color the patch.
All together:
c = fnplt(cscvn(xyz(:,[1:end 1])),'r',2);
colormap gray
fill3(c(1,:),c(2,:),c(3,:),1:size(c,2))
the result:

Related

Excluding NaNs from data sorting for box plot with overlaid data

I'm plotting a box plot with overlaid data from the following concatenated matrix:
data = [10 16 24 31 12 26 23 33;11 15 27 27 12 24 22 36;12 15 24 25 14 25 22 37;10 16 27 24 14 27 23 41;12 15 NaN NaN 15 NaN 22 NaN;13 18 NaN NaN 16 NaN 22 NaN]
The code for this plot is:
datas=sort(data);
datainbox=datas(ceil(end/4)+1:floor(end*3/4),:);
[n1,n2]=size(datainbox);
dataoutbox=datas([1:ceil(end/4) floor(end*3/4)+1:end],:);
n3=size(dataoutbox,1);
% calculate quartiles
dataq=quantile(data,[.25 .5 .75]);
% calculate range between box and outliers = between 1.5*IQR from quartiles
dataiqr=iqr(data);
datar=[dataq(1,:)-dataiqr*1.5;dataq(3,:)+dataiqr*1.5];
dataoutbox(dataoutbox<ones(n3,1)*datar(1,:)|dataoutbox>ones(n3,1)*datar(2,:))=nan;
figure()
hold on
bp = boxplot(data);
plot(ones(n1,1)*[1 2 3 4 5 6 7 8]+.4*(rand(n1,n2)-.5),datainbox,'k.','MarkerSize',12)
plot(ones(n3,1)*[1 2 3 4 5 6 7 8]+.4*(rand(n3,n2)-.5),dataoutbox,'.','color',[1 1 1]*.5,'MarkerSize',12)
set(bp,'linewidth',1);
As indicated above, I am sorting the data into 'datainbox' and 'dataoutbox' based on the IQR. The code works as expected (credit to JJM Driesson) except for the data columns containing NaNs, where as shown in the plot the data is not sorted correctly. How should I modify the above code to exclude NaNs from calculations and prevent this from influencing the plot?
Thank you for your time,
Laura
You should process every column separately. You can select the NaN values as follows: col = data(~isnan(data(:, i)), i);
If you want all the boxplots in the same figure, you can try to use this answer.

MATLAB - Error bars separation distance and height

I have data arrays w and x; I want to plot error bars y distance apart and z distance above and below the points. Is there a way to do this? I've tried manipulating the errorbar function but can't figure it out.
w [1
3
5
8
9
15
17
34
67
79
90
123
63
23
2
]
x[1
2
3
4
5
6
7
8
9
10
11
12
13
14
15]
plot(x,w)hold on;
errorbar(x,w....not sure what to put after);
I'm trying to plot error bars every 3rd point and with a height of +-5
You could simply draw the error bars yourself
for idx = 1:3:length(w)
plot([x(idx) x(idx)],[w(idx)+5 w(idx)-5]);
end
Alternatively you could give a handle to the errorbar function, but I'm not sure if it allows you to modify this stuff.
By setting the right Properties of the errorbarobject you can get what you need.
Note the LData and UData properties, which are used to specify the height below and above the bars as well as the XData and YData.
clear
clc
close all
w = [1 3 5 8 9 15 17 34 67 79 90 123 63 23 2 ];
x = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
%// Set location on x axis
loc = 1:3:numel(w);
plot(x,w)
hold on;
hErr = errorbar(loc,w(loc),'rx','LData',5,'UData',5,'XData',loc,'YData',w(loc));
Output:

How to show only the existing data points on x axis of bar graph in MATLAB?

I need to simple plot B vs. A as bar plot in MATLAB, but I don't want my x axis showing completely from 1 to 274. I only need to show the existing data point on my x axis, which can be done easily in Excel as in the image below. How can MATLAB do this?
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 20 25 27 29 37 40 42 43 48 73 204 242 274];
B=[30 15 5 9 5 6 3 3 2 1 4 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1];
You need to set both 'XTick' and 'XTickLabel' axes properties:
bar(B);
set(gca,'XTickLabel',A)
set(gca,'XTick',1:numel(A));
xlim([0 numel(A)+1]);
Here is an inelegant, but, nonetheless, working solution to your question:
x = [1,4, 6, 7]; % Your data
uni = unique(x)
yMax = length(find(x == mode(x))) + 1;
c = cell(1, length(uni));
c = strread(num2str(uni),'%s')
hist(1:length(uni));
axis([0 length(uni) 0 yMax])
set(gca, 'XTick', 1:length(uni));
set(gca, 'XTickLabel', c);
Basically, this plots the histogram as if the data were spread from 1 to the number of unique elements. Then, it sets the tick marks at each histogram value. Then, it labels each tick mark with the correct number.

How can I visualize/plot temperature gradient using matlab?

I have data at specific points in 3D rectangle and I want to see temperature gradient. I have values at specific points , but I want a continous flow of gradient between each sensor. I am not been able to figure out how to visualize or map data in between each sensors placed at different points. stucked :(
X=[5 0 0 0 0 5 10 10 10 10 0 5 10 10 0 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 0 5 10 0 5 10 10 10 5 0 0]';
Y=[10 10 5 5 10 10 5 10 5 10 0 0 0 0 0 0 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 0 0 0 0 0 0 5 10 10 10 5 ]';
Z=[20 20 20 14 14 14 14 14 20 20 20 20 20 14 14 14 3.8 3.8 0 0 7.5 7.5 10 10 12.5 12.5 15 15 17.5 17.5 20 20 0 0 0 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5]';
%# temperature vector
T = [20 22 24 22.1 26.1 22.4 15 17 21 22 19 22 18 17 18 20 21 22 21 24 22.3 22.5 22.8 28.9 22 27 26 20 19 24 21 23 19 18 22 25 27 21 29 25 22 21 22];
scatter3(X,Y,Z,[4000],T,'.');
grid off
box off
view(32,18); axis equal tight off vis3d; % azimuth 26
camproj perspective
camlight; lighting gouraud; alpha(0.75);
rotate3d on
Code below just shows how my one side of 3d rectangle should look like(its just a random code)
datagrid = 500*peaks(100);
R = makerefmat('RasterSize',size(datagrid));
[aspect,slope,gradN,gradE] = gradientm(datagrid,R);
figure; axesm eqacyl
meshm(datagrid,R)
colormap (jet(64))
colorbar('vert')
title('Peaks: elevation')
axis square
You can break the problem down into two sub-problems:
Interpolation
Visualization
Let's take a look at interpolation first. There are many methods available but let's try the MATLAB function griddatan. This will interpolate (linearly) values onto a new set of points (here I've used a regular grid constructed using meshgrid).
M = 20;
N = 20;
L = 40;
T = transpose(T);
% Set up the grid of points we wish to interpolate at
[xi,yi,zi] = meshgrid(linspace(0,10,M),linspace(0,10,N),linspace(0,20,L));
% Perform interpolation
ti = griddatan([X,Y,Z],T,[xi(:),yi(:),zi(:)]);
% Reshape back from a column vector to a MxNxL matrix
ti = reshape(ti, size(xi));
% Visualise in some way
scatter3(xi(:),yi(:),zi(:),400,ti(:),'.')
When it comes to visualization then the sky's the limit and 3D volume visualization is more of an art than a science. I'm afraid I can't run your example (I don't have access to makerefmat) but http://www.mathworks.co.uk/help/techdoc/visualize/bqliccy.html has some good starting points.

How to plot a 3d surface graph in MATLAB?

I have a dataset like so:
| 0.1 0.2 0.3 0.4
----------------------
1 | 10 11 12 13
2 | 11 12 13 14
3 | 12 13 14 15
4 | 13 14 15 16
I want to plot a 3D surface graph in matlab such that the column headings will be on the y axis, the row headings will be on the x axis and the remaining values will determine the height of the point on the z axis.
I have had a look around at lots of different example and I can't work out how to achieve this. At the moment I have got the following:
Y = [0.1 0.2 0.3 0.4];
X = [1 2 3 4];
Z = [10 11 12 13; 11 12 13 14; 12 13 14 15; 13 14 15 16];
Please could someone help me out?
surf(X,Y,Z)
May a bar plot yield the desired picture?
Y = [0.1 0.2 0.3 0.4];
X = [1 2 3 4];
Z = [10 11 12 13; 11 12 13 14; 12 13 14 15; 13 14 15 16];
figure;
bar3(Z)
set(gca(gcf), 'xticklabel',{'0.1','0.2','0.3','0.4'})