I have a 420*940*12 matrix (the value of each grid ranges from 0-100) and I want to plot data only inside polygon S, with lat (420*1) and lon (940*1) as grid references.
We can create a single plot of only points without its values (0-100):
S = shaperead(polygon);
N = length(S);
[X,Y] = meshgrid(lon,lat);
data= test;
for k = 1:N
idx = insidepoly(X(:),Y(:),S(k).X,S(k).Y);
hold on
plot(X(idx), Y(idx), 'r*')
end
But this figure is only showing the points not its values (0-100)
And the output is something like this:
But I have to plot multiple data together, lets say 12 months of a year so, how can I make 12 plots together according to values (0-100) as in the below figure?
enter image description here
one possible solution is to use montage, see here:
img = imread('peppers.png');
img = repmat(img,1,1,1,6);
montage(img)
Related
I am trying to plot data from a csv file, I have successfully uploaded the csv as an array for each column. I'm then trying to find the peaks of each column, I need to then plot the peaks for each to a time constant (tau). I have been getting an error that I need to have the same size vectors to plot. I'm also trying to plot all sets of data on one plot and have a curve fit for each.
Please help me out!
code is below
Array=csvread("D:\Grad Lab\NMR\Data\T1 Data\compiledT1nolabel.csv");
tau = Array(:,1);
Water= Array(:,2);
Mineral_Oil = Array(:,3);
Glycerol = Array(:,4);
CuSO4_1=Array(:,5);
CuSO4_2=Array(:,6);
CuSO4_3=Array(:,7);
CuSO4_4=Array(:,8);
CuSO4_5=Array(:,9);
pks1 = findpeaks(Water);
pks2 = findpeaks(Mineral_Oil);
pks3 = findpeaks(Glycerol);
pks4 = findpeaks(CuSO4_1);
pks5 = findpeaks(CuSO4_2);
pks6 = findpeaks(CuSO4_3);
pks7 = findpeaks(CuSO4_4);
pks8 = findpeaks(CuSO4_5);
plot(pks1,tau)
The problem is that the pks vectors and the corresponding data arrays you are trying to plot don't have the same size: calling the plot function on vectors of different sizes produces the error.
In the sample script below, data contains a bunch of values. Invoking findpeaks with two outputs (i.e. locs and pks) allows both the locations and peak values to be stored. Now we can use locs and pks with plot to mark the peaks as follows:
data = [25 8 15 5 6 10 10 3 1 20 7];
[pks, locs] = findpeaks(data);
plot(data); hold
plot(locs, pks, 'd')
produces the following plot:
Alternatively, if you are just interested in plotting, you could use
findpeaks(data)
without any output: it will produce a plot like this:
For the OP's specific case, I modified his script as follows (just covered one set of peaks, the pattern should be clear):
Array=csvread("D:\Grad Lab\NMR\Data\T1 Data\compiledT1nolabel.csv");
tau = Array(:,1);
Water = Array(:,2);
[pks1, locs1] = findpeaks(Water);
figure
plot(tau, Water); hold on;
plot(tau(locs1), pks1, 'd');
I have a number of 2d probability mass functions from 2 categories. I am trying to plot the contours to visualise them (for example at their half height, but doesn't really matter).
I don't want to use contourf to plot directly because I want to control the fill colour and opacity. So I am using contourc to generate xy coordinates, and am then using fill with these xy coordinates.
The problem is that the xy coordinates from the contourc function have strange numbers in them which cause the following strange vertices to be plotted.
At first I thought it was the odd contourmatrix format, but I don't think it is this as I am only asking for one value from contourc. For example...
contourmatrix = contourc(x, y, Z, [val, val]);
h = fill(contourmatrix(1,:), contourmatrix(2,:), 'r');
Does anyone know why the contourmatrix has these odd values in them when I am only asking for one contour?
UPDATE:
My problem seems might be a failure mode of contourc when the input 2D matrix is not 'smooth'. My source data is a large set of (x,y) points. Then I create a 2D matrix with some hist2d function. But when this is noisy the problem is exaggerated...
But when I use a 2d kernel density function to result in a much smoother 2D function, the problem is lessened...
The full process is
a) I have a set of (x,y) points which form samples from a distribution
b) I convert this into a 2D pmf
c) create a contourmatrix using contourc
d) plot using fill
Your graphic glitches are because of the way you use the data from the ContourMatrix. Even if you specify only one isolevel, this can result in several distinct filled area. So the ContourMatrix may contain data for several shapes.
simple example:
isolevel = 2 ;
[X,Y,Z] = peaks ;
[C,h] = contourf(X,Y,Z,[isolevel,isolevel]);
Produces:
Note that even if you specified only one isolevel to be drawn, this will result in 2 patches (2 shapes). Each has its own definition but they are both embedded in the ContourMatrix, so you have to parse it if you want to extract each shape coordinates individually.
To prove the point, if I simply throw the full contour matrix to the patch function (the fill function will create patch objects anyway so I prefer to use the low level function when practical). I get the same glitch lines as you do:
xc = X(1,:) ;
yc = Y(:,1) ;
c = contourc(xc,yc,Z,[isolevel,isolevel]);
hold on
hp = patch(c(1,1:end),c(2,1:end),'r','LineWidth',2) ;
produces the same kind of glitches that you have:
Now if you properly extract each shape coordinates without including the definition column, you get the proper shapes. The example below is one way to extract and draw each shape for inspiration but they are many ways to do it differently. You can certainly compact the code a lot but here I detailed the operations for clarity.
The key is to read and understand how the ContourMatrix is build.
parsed = false ;
iShape = 1 ;
while ~parsed
%// get coordinates for each isolevel profile
level = c(1,1) ; %// current isolevel
nPoints = c(2,1) ; %// number of coordinate points for this shape
idx = 2:nPoints+1 ; %// prepare the column indices of this shape coordinates
xp = c(1,idx) ; %// retrieve shape x-values
yp = c(2,idx) ; %// retrieve shape y-values
hp(iShape) = patch(xp,yp,'y','FaceAlpha',0.5) ; %// generate path object and save handle for future shape control.
if size(c,2) > (nPoints+1)
%// There is another shape to draw
c(:,1:nPoints+1) = [] ; %// remove processed points from the contour matrix
iShape = iShape+1 ; %// increment shape counter
else
%// we are done => exit while loop
parsed = true ;
end
end
grid on
This will produce:
pointA=[9.62579 15.7309 3.3291];
pointB=[13.546 25.6869 3.3291];
pointC=[23.502 21.7667 -3.3291];
pointD=[19.5818 11.8107 -3.3291];
points=[pointA' pointB' pointC' pointD'];
fill3(points(1,:),points(2,:),points(3,:),'r')
grid on
alpha(0.3)
This code will show a filled plane(Cant add images yet T.T)
Now here is my problem. On a spreadsheet, I have x,y,z coordinates of thousands of points. The 4 consecutive points form a plane like the one shown. How do I make a code such that for every 4 consecutive points, it makes a filled plane.
Basically, if I have 400 points, I want the code to plot 100 planes.
Assuming your data are a matrix, m = (400,3)
m = rand(400,3);
for i = 1:length(m);
m2 = m'; % Transpose
end
Create a 3-D matrix in which 'j' represents each set of points:
m3=[];
%Not the most elegant way to cycle through every four points but it works!
z = 0:(length(m2)/4); z1 = (z*4)+1; z1 = z1(:,1:length(z)-1);
for j = 1:length(z1);
m3(:,:,j) = m2(:,z1(j):(z1(j)+3));
end
'j' now has a total length = 100 - representing the amount planes;
fill3(m3(1,:,1),m3(2,:,1),m3(3,:,1),'r');
% Cycle through planes- make a new figure for each plane;
for j = 1:length(z1);
fill3(m3(1,:,j),m3(2,:,j),m3(3,:,j),'r');
end
clear all, close all, clc
pointA=rand(99,1);
pointB=rand(99,1);
pointC=rand(99,1);
pointD=rand(99,1);
pointAmat = reshape(pointA,3,1,[]);
pointBmat = reshape(pointB,3,1,[]);
pointCmat = reshape(pointC,3,1,[]);
pointDmat = reshape(pointD,3,1,[]);
points=[pointAmat pointBmat pointCmat pointDmat];
for i = 1:size(points,3)
fill3(points(1,:,i),points(2,:,i),points(3,:,i),'r')
hold all
end
grid on
alpha(0.3)
Hope this helps.
I'm trying to make a color plot in matlab using output data from another program. What I have are 3 vectors indicating the x-position, y-yposition (both in milliarcseconds, since this represents an image of the surroundings of a black hole), and value (which will be assigned a color) of every point in the desired image. I apparently can't use pcolor, because the values which indicate the color of each "pixel" are not in a matrix, and I don't know a way other than meshgrid to create a matrix out of the vectors, which didn't work due to the size of the vectors.
Thanks in advance for any help, I may not be able to reply immediately.
If we make no assumptions about the arrangement of the x,y coordinates (i.e. non-monotonic) and the sparsity of the data samples, the best way to get a nice image out of your vectors is to use TriScatteredInterp. Here is an example:
% samplesToGrid.m
function [vi,xi,yi] = samplesToGrid(x,y,v)
F = TriScatteredInterp(x,y,v);
[yi,xi] = ndgrid(min(y(:)):max(y(:)), min(x(:)):max(x(:)));
vi = F(xi,yi);
Here's an example of taking 500 "pixel" samples on a 100x100 grid and building a full image:
% exampleSparsePeakSamples.m
x = randi(100,[500 1]); y = randi(100,[500 1]);
v = exp(-(x-50).^2/50) .* exp(-(y-50).^2/50) + 1e-2*randn(size(x));
vi = samplesToGrid(x,y,v);
imagesc(vi); axis image
Gordon's answer will work if the coordinates are integer-valued, but the image will be spare.
You can assign your values to a matrix based on the x and y coordinates and then use imagesc (or a similar function).
% Assuming the X and Y coords start at 1
max_x = max(Xcoords);
max_y = max(Ycoords);
data = nan(max_y, max_x); % Note the order of y and x
indexes = sub2ind(size(data), max_y, max_x);
data(indexes) = Values;
imagesc(data); % note that NaN values will be colored with the minimum colormap value
I want to make a plot in Matlab like this.
How could I do something like this in Matlab?
Thank you all!
Make some data:
x = 1:0.1:100;
y = 1:5;
for i = y
z(i,:) = sin(i*x);
end
Plot it:
figure
hold on
for i = y
plot3(x,i*ones(size(x)),z(i,:))
end
Modify the plot aspect and view:
daspect([100,2,2])
view(45,60)
Does that do roughly what you need?
You can use the command plot3(X,Y,Z).
You have to build three matrices each one containing a number of column equal to the number of series you need. (6 in the figure you sent)
For example
X = repmat([-200:200]',1,6);
Z = rand(401,6)*10;
Y = ones(401,1)*[1:20:120];
plot3(X,Y,Z)
axis image