I am plotting some discrete values with a stem plot in MATLAB. I found that if the value is zero, the stem plot will put a circle on the x axis to show the zeros. Is there a way to have a stem NOT showing the circles if the value is zero?
Treat them as NaN's, ie:
Y = [1;2;3;0;3;2;4;0;1];
Y(Y == 0) = NaN;
stem(Y);
The 4th and 8th index will still exist on the x-axis, but if the observation is set to NaN, no line or circle will be plotted.
Related
I am having some trouble understanding contours.
What I have understood so far is that contours are a way to represent a 3d figure in a 2d plane. It does so by plotting a function of 2 variables as curves along which the function has same value.
Now if I do:
z=[1 4; 10 7];
contour(z);
I get this:
I read the documentation and it says:
contour(Z) draws a contour plot of matrix Z, where Z is interpreted as
heights with respect to the x-y plane. Z must be at least a 2-by-2
matrix that contains at least two different values. The x values
correspond to the column indices of Z and the y values correspond to
the row indices of Z. The contour levels are chosen automatically.
Thus for x=1,y=1: z=1, x=2,y=1: z=4 and so on. However I can't understand how to interpret this as the contour plot shown above.
And if I write:
contour(X1, X2, vals, [0.5 0.5], 'b'); where X1, X2 and vals are equal sized matrices and vals is a matrix of only 0s and 1s. I can't understand what does the argument [0.5 0.5] do. I read the documentation which states:
contour(Z,v) draws a contour plot of matrix Z with contour lines at
the data values specified in the monotonically increasing vector v. To
display a single contour line at a particular value, define v as a
two-element vector with both elements equal to the desired contour
level.
and I am unable to understand this statement.
The problem of the first contour is that there are just 4 values. Try something like
x = 0:0.1:10;
y = 0:0.1:10;
z = sin(x') * cos(y);
contour(z)
For the second thing, this means that if you want to see just particular contours, input them as vector v. In the example above:
contour(z, [0.1, 0.2, 0.3])
will show contour lines of 0.1, 0.2 and 0.3.
To have a single contour line, you can't have just (z, 0) but require (z, [0,0])
I want to set the Y axis beginning value equal to 1e-20, but MATLAB always gives zero. My variable, which is meanerror has a value of 1e-15. So MATLAB's distances between the Y axis values are very big and whatever I do, I don't see 1e-15 in this graph.
Actually,I want to see 1e-15 after beginning any value which is not zero on Y axis. May be between 1e-20 (beginning value) and 1e-10.
How can I do that ?
Here is the code :
plot(x,meanerror,'Color','k');
set(gca, 'Ticklength', [0 0]);
set(gca,'xlim',[0 3e5]);
set(gca,'XTickLabel',{'0','','1E+5','','2E+5','','3E+5'});
set(gca,'ylim',[1e-20 1e3]);
Here is the graph:
Given the following code, how would one make the Inf values invisible in the scatter plot without color manipulation?
J = rand(20, 40, 5);
J(J>.6 & J<.4) = Inf;
% Plot a scatter matrix
shape = size(J);
[x,y,z] = meshgrid(1:shape(1), 1:shape(2), 1:shape(3));
scatter3(x(:), y(:), z(:), 4, J(:), 'fill');
Data that have NaN values are made invisible when plotting with MATLAB, which you can exploit in your case. Since you want to make the Infinte values as invisible, you can convert all those to NaNs and then plot them. Here you can take help of logical indexing to index into Inf element positions. Thus, the code would be -
J(isinf(J))=NaN
%// ... Plot J
One method could be to change the values higher than a certain threshold to NaN (or any other number). I believe NaN values will not show up in your scatter plot. You can do this with the same code you are already using.
J(J>10^6) = NaN;
How can I plot a map area filled with colors and colors representing the values which are negative and positive. Colormap can only give colors to values in the range [0,1]. I want to increase the range.
Let x be the matrix with the values you want to visualize. Then you can use imagesc for a visualization:
x = (rand(100,100)-0.5)*10; % random values between -5 and 5
imagesc(x);
colorbar
Note that the colorbar is automatically adjusted to the range of date in x.
The simple way would be to do this:
Suppose you want to plot x
xpos=x;
xneg=x;
xpos(xpos<0)=NaN;
xneg(xneg>=0) = NaN;
plot(xpos)
hold all;
plot(xneg)
Hi can somebody help me with the Matlab command here. I've got to determine the highest point in a filled contour I've plotted using matrix data in a file. And then I have to mark the highest point with a red x.
load('0101862_mod.dtm') % loading the dtm file
X = X0101862_mod(1:81,:) % we name X0101862, it is the location where the data X, Y and Z is stored
Y = X0101862_mod(82:162,:)
Z = X0101862_mod (163:243,:)
figure (1)
subplot(2,2,3)
[C,h] = contourf(X,Y,Z,10);
xlabel('x'); ylabel('y'); zlabel('z'); title('X0101862_mod');
view(-73,34); axis equal; colormap summer; colorbar;
i know it involves max command. Kept getting error when i use max.
To plot the red 'X', you have to call first hold on to make sure that the second plotting command won't erase the contour. Then, you use plot(xMax,yMax,'xr') to plot a red 'x' at the x/y coordinates where z is at its maximum.
To find xMax and yMax, you have to use the second output argument of max. MAX returns, as first output, the maximum (e.g. of Z), and as a second output, it returns the number of the element that is maximal. Use that number (the index) to find the elements in X and Y that correspond to the maximum Z-value, i.e. xMax and yMax.