I am trying to combine two "solved" aspects of MATLAB -- 1) plotting a 2D line with color that varies along the line in 2) polar axes.
The first part is usually easy, and frequently asked:
https://www.mathworks.com/matlabcentral/answers/5042-how-do-i-vary-color-along-a-2d-line or
How to vary the line color of a matlab plot (like colormap)?
The most commonly suggested trick is to use surf or mesh to create a "fake" 3D line and color this. However, this is not supported on polaraxesin MATLAB:
>> polaraxes, hold on;
>> surf([1 1; 1 1], [2 2; 2 2], [3 3; 3 3])
Error using newplot (line 80)
Adding Cartesian plot to polaraxes is not supported.
One trick that does seem to work is using a sequence of line segments, as is done in cline.m from File Exchange. http://www.mathworks.com/matlabcentral/fileexchange/3747-cline-m
>> polaraxes; hold on; cline;
Gives this
which is technically what I want... but as pointed out in the previous comments, is much uglier than the solution with surf or mesh since it draws individual segments.
Is there any other way to do this? I found this question also asked here
https://www.mathworks.com/matlabcentral/answers/439176-how-do-i-vary-the-color-along-a-line-in-polar-coordinates
with an "accepted answer" that this does not seem possible, so I'm feeling a little pessimistic.
Cross-posting the answer from Chad Greene here
https://www.mathworks.com/matlabcentral/answers/822360-color-along-line-in-polaraxes#answer_692780
theta = linspace(0,6*pi,100000);
rho1 = theta/10;
polarscatter(theta,rho1,5,rho1,'filled')
Looks as good as its going to get, I think.
One way to make it "prettier" and not use so few lines is to increase the number of lines that is used to plot the line. I looked into the file of cline and where x is assigned with 101 points is where you could modify it to get the example to be prettier so that it doesn't look like it is built with straight lines, even if it is so.
So by changing x=linspace(-10,10,101) to x=linspace(-10,10,1001) the example polaraxes; hold on; cline; gets prettier. This means that you could still use cline with your own values of x, y, z, c and get a pretty polaraxes with your choice of colormap if you make the lines small enough, e.g. many points in your interval for x.
Related
Is there is elegant way to connect the line between the nearest points in scatter plot?
The reason I ask is because plot will connect the line based on the 'index of row' of Y(data). Basically, it connects points in the same row in Y. However, this sometimes will result in a jump, which is due to the missing data in one row( and thus all the following data will all mislabeled and shifted by 1, and this cannot be avoided for some practical reason).
Here is a minimal example of the problem.
xrange=linspace(-2,2,100);
Y=repmat(-xrange.^2,4,1)+repmat((-4:-1)',1,100);
Y(Y<-5)=0;
for i=1:100
[~,~,v]=find(Y(:,i));
Y(1:length(v),i)=v;
end
Y(Y==0)=nan;
%jump due to missing data
figure;
plot(xrange,Y);
figure;
%from bare eye, we see there are four lines
for i=1:4
scatter(xrange,Y(i,:),'b');
hold on
end
The undesirable result by using plot is:
The jump is due to the missing data and this is unavoidable in practice.
However, we can see that there are actually four lines, by bare eyes, if we use scatter.
So what I want to achieve is to connect the nearest points but without introducing discontinuity given a imperfect data set, which is missing some data. What I can come up with is to preprocess the data before plotting but this is not always possible, because of the complicated experimental situation, which is hard to predict which data point will be missing in advance.
Any comments and answers are highly appreciated!
idx = ~isnan(Y)
plot(xrange(idx), Y(idx))
or better yet, since you know you want to get rid of everything less than -5
idx = Y < -5
plot(xrange(idx), Y(idx))
I am trying to create a plot using the below code, the plot it produces incorporates values of the x-axis from 0.5-1, however I need the plot to not incorporate this part on the x-axis, from values 0.5-1. I assumed that lines 9 & 10 of the code would exclude this part of the plot, however this is not the case.
Does anyone know what I would write for this part of the plot to be excluded. Thank you!
x = [0:0.01:1];
y = [0:0.01:1];
z = size(101,101);
for j = 1:101;
for k = 1:101;
z(j,k)=((x(j))./(0.5-y(k)));
if z(j,k)>1
z(j,k)=1;
elseif z(j,k)<0
z(j,k)=1;
end
end
end
surf(x,y,z)
xlim([0 .5]) sets the range to be displayed on the x axis. Use it after your plot command (surf).
x=[0:0.005:0.5];
This would exclude the data you do not want to see in the plot, but would give you better resolution for your answer.
MWc answer would work as well, but would just exclude the values from 0.5 on.
So it depends on exactly what you wanted to see given your overall problem.
So I have this matrix here, and it is of size 13 x 8198. (I have called it 'blah').
This is a sparse matrix, in that, most of its entries are 0. When I do an imagesc(blah), I get the following image:
Clearly this is worthless because I cannot clearly see the non-zero elements. I have tried playing around with the color scaling, but to no avail.
Anyway, I was wondering if there might be a nicer way to be able to visualize this matrix in MATLAB somehow? I am designing an algorithm and would like to be able to see certain things int teh matrix.
Thanks!
Try spy; it's intended for exactly that.
The problem is that spy makes the axes equal, and your data is 13 x 8198, so the first axis is almost invisible compared to the second one. daspect can fix that.
>> spy(blah)
>> daspect([400 1 1])
spy doesn't have an option to plot differently by signs. One option would be to edit the source to add that capability (it's implemented in matlab, and you can get the source by running edit spy). An easier hack, though, is to just spy the positive and negative parts separately:
>> daspect([400 1 1]);
>> hold on;
>> spy(max(blah, 0), 'b');
>> spy(min(blah, 0), 'r');
This has the unfortunate side effect of making places where positives and negatives are close together appear dominated by the second one plotted, here the negatives (e.g. in the top rows of your matrix). I'm not sure what to do about that other than maybe fiddling with marker sizes. You could of course do it in both orders and compare.
I have a 1 x 10 vector as shown below:
V = [0.500 -5.433 0.543 0.321 1.432 0.543 -0.576 -0.145 -1.322 -0.222]
and want to plot this on MATLAB using plot.
I used plot(v,0,'kx,'marker',10) but it does not seem reasonable for me. Any idea on how to go about with this?
Does anyone have a very good resource for for ISOMAP? Need a very comprehensive step by step easy tutorials on Isomaps. If i can have good videos on it will be very good.
you almost got it, just write:
plot(v,'kx','MarkerSize',10);
note, that I wrote plot and not Plot, Matlab is case sensitive...
when you only have a single vector the plot function assumes that for the x-axis it takes the number of elements of the vector, i.e. plot(1:numel(v),v,...). I recommend you to use the Matlab documentation, if you had read it, you'd see an example that could show you what you did wrong.
bla's solution is okay and produces a scatter plot.
However, you can make a line plot, etc.
Though, notice it is V and not v, as he pointed out that MATLAB is case-sensitive.
Here is how to generate a line plot:
x=0:length(V)
plot(x,V,'r--o',x,V,'r*')
Output:
why when i use ezplot in for example [1 1.5] interval, a discontinuity will appear in some pieces of lines but when i use a closer interval like [1.3 1.5], the discontinuity will be annihilated?
EZPLOT is a general purpose plotting function that will automatically select a set of points at which to evaluate and plot the function you pass to it. Most of the time, things work fine. But there are some special cases where EZPLOT can have some trouble. It may not render well near discontinuities or points where there are rapid changes in the function (which it may mistake for a discontinuity).
That's the drawback of a function that is designed to be general enough to accept any function you give it: it's hard to make it general enough to handle everything exactly right, so some special edge cases look a little funny. In such cases, you should avoid functions like EZPLOT (which make a lot of choices for you) and plot things yourself by evaluating your functions at points you choose and plotting those points using the PLOT function. Here's a helpful link for this.
The problem is that ezplot() is useful, but not that robust.
A better option for plotting a function without discrete points is fplot(). Check out the documentation for it.
Here is an example of how to use it compared with ezplot():
lowerBound = 0;
upperBound = 1;
%# The ezplot way:
ezplot('y=sin(1/x)',[lowerBound,upperBound,-1,1])
%# The fplot way:
fplot('sin(1/x)',[lowerBound,upperBound])
fplot() will evaluate more points where the function changes more rapidly. Thus discontinuities will still cause problems in the graph if you look closely, but it will try harder to plot them accurately.
To plot a level curve of a function with three variables requires a little more typing:
%# First create a grid where you want the function to be drawn
[x,y]=meshgrid(-2:.01:2);
%# Remember that -2:.01:2 creates a vector with values from -2 to 2
%# in steps of .01
%# Then define your function
z=-3*y./(x.^2+y.^2+1);
%# Now graph the level curve of the function. I chose the level z=0.5:
contour(x,y,z,[0.5])