How can i specify the line colour using gplot in MATLAB? - matlab

I would like to plot a graph in MATLAB using gplot.
In the documentation for gplot, it says the proper syntax is:
gplot(A,Coordinates,LineSpec)
so if I do something like:
gplot(A,XY,'-or');
it will plot the graph in red with circles for the vertices. My problem is that i would like to plot it in grey, however the documentation for LineSpec seems to only allow line colours in the set: {r,g,b,c,m,y,k,w}; i can't seem to find anywhere in the documentation that lets you specify the line colour using an RGB triplet.
Am i just missing something?

Looking through the gplot code it's a bit strangely designed. It uses the standard plot function but the logic it uses to get the line spec precludes the use of PV pairs that work with plot. Other than modifying the code I don't see a way to specify the Color property with gplot like you can with a normal line plot.
However, there is undocumented behavior in gplot that will allow you to plot the data normally yourself using a standard plot call. From the code:
% [X,Y] = GPLOT(A,xy) returns the NaN-punctuated vectors
% X and Y without actually generating a plot. These vectors
% can be used to generate the plot at a later time if desired. As a
% result, the two argument output case is only valid when xy is of type
% single or double.
So we can get our XY data and plot ourselves:
k = 1:30;
[B,XY] = bucky;
[X, Y] = gplot(B(k,k),XY(k,:));
plot(X, Y, '-*', 'Color', [0 1 1]);
axis square

Related

Matlab logarithmic range colorbar imagesc

Using the function imagesc in Matlab, I plot my (X,Y, Z) data-X array distance, Y array time, and my data Z = Z(X,Y) a matrix.
I notice that the 80% of the image has one color, because the change of Z data occurred only in the end of X for almost all Y.
Right now I use colormap('hsv') which give I think the largest range of different colors.
I need to change the colorbar range to a logarithmic one to improve visual the range of the change of my output data through time along the distance X.
I have used also contourf but still I am not sure if it will better to use this function and not imagesc which the output is more smoothed.
Please, any idea, any method or any small script that I could use to show visual the difference in data in logarithmic scale in 2D using imagesc or another build in function is more than welcome!
thank you
There is a discussion at the Mathworks website where someone provided a function that does logarithmic color bars.
https://www.mathworks.com/matlabcentral/newsreader/view_thread/152310
EDIT: copying and pasting code from link
function cbar = colorbar_log(my_clim)
%COLORBAR_LOG Apply log10 scaling to pseudocolor axis
% and display colorbar COLORBAR_LOG(V), where V is the
% two element vector [cmin cmax], sets manual, logarithmic
% scaling of pseudocolor for the SURFACE and PATCH
% objects. cmin and cmax should be specified on a LINEAR
% scale, and are assigned to the first and last colors in
% the current colormap. A logarithmic scale is computed,
% then applied, and a colorbar is appended to the current
% axis.
%
% Written by Matthew Crema - 7/2007
% Trick MATLAB by first applying pseudocolor axis
% on a linear scale
caxis(my_clim)
% Create a colorbar with log scale
cbar = colorbar('Yscale', 'log');
% Now change the pseudocolor axis to a log scale.
caxis(log10(my_clim));
% Do not issue the COLORBAR command again! If you want to
% change things, issue COLORBAR_LOG again.

Using streamslice with color for velocities

I am using streamslice command to visualize my flow. I want to add color depending on magnitude of velocities but there seems to be no function argument in streamslice to do so. The function is given as:
% x - x-coordinates
% y - y-coordinates
% u,v - vector volume data
h = streamslice(x,y,u,v)
The function produces this image
If you want to use streamslice, I can suggest something. It would need some tweaking to get it look like a cool figure, but it does the job, I guess. the idea is to combine a surf plot with the streamsilces plot.
Look at the result. I guess that with better a colormap and with some tricks getting the data handle of the streamsliceto change the line colour it could work nicely, speciall yin Matlab R2014b or higher.
CODE:
clear;clc;
load wind
% Use only a piece of this datasheet
x=x(:,:,5);
y=y(:,:,5);
u=u(:,:,5);
v=v(:,:,5);
mag=sqrt(u.^2+v.^2);
figure
hold on
surf(x,y,mag-max(mag(:)),'FaceColor','interp','Edgecolor','none')
colormap('hot')
streamslice(x,y,u,v)
axis([min(x(:)) max(x(:)) min(y(:)) max(y(:)) -max(mag(:)) 0])

MATLAB contour plot of 2D scatter

What I want to do is very simple, I just cannot seem to get MATLAB to do it. I would like to plot contours using my 2D data set.
My data set is large; 2 x 844240. I can do a scatter plot just fine,
scatter(Data(1,:), Data(2,:));
Reading through the forums I found Scatter plot with density in Matlab, where a hisogram was plotted. This would suffice, however, I would like to overlay the plots.
The issue is that they have different axis, my scatter data has an axis of [0 0.01 0 2500]; whereas the histogram is [0 100 0 100].
Is there a way to change the axis values of the histogram without modifying the image?
Thanks!
If I understand correctly, you are using hist3 to construct a histogram and then using imagesc to plot it. You can use the second output argument of hist3 to get the histogram bin centers, and then pass those on to imagesc, e.g.
nBins_x = 100;
nBins_y = 100;
[counts, bin_centers] = hist3(Data, [nBins_x nBins_y]);
x_bin_centers = bin_centers{1};
y_bin_centers = bin_centers{2};
imagesc(x_bin_centers, y_bin_centers, counts)
A couple other notes:
In your case, you will need to transpose your [2 x N] matrix when passing it to hist3, which expects an [N x 2] matrix.
imagesc puts the first axis (which I've been calling the "x" axis) on the vertical axis and the second on the horizontal axis. If you want to flip it, you can use:
imagesc(y_bin_centers, x_bin_centers, counts')
If you want to specify the histogram bins explicitly (e.g. to match your scatterplot) you can specify that in the arguments to hist3:
x_bin_centers = linspace(0, .01, 100);
y_bin_centers = linspace(0, 2500, 100);
counts = hist3(Data, {x_bin_centers, y_bin_centers};
And if you want a contour plot, you can use (note that contour takes the axes arguments in a different order than imagesc):
contour(x_bin_centers, y_bin_centers, counts');
If you are unhappy with the jaggedness of the contours, you may consider using a kernel density estimate instead of a histogram (check out ksdensity) (oops, looks like ksdensity is 1-D only. But there are File Exchange submissions for bivariate kernel density estimation).

mathcad / matlab 3D plot of transfer function

I have a problem plotting a 3D plot of my transfer function. In matlab I have tryed this:
[T,w] = meshgrid(1:1:32,1:1:100);
sys2=20*log((1-w.*(T./2)./w.*T).*(((2.56.*(w.^2)+1.6.*w+1)./(0.0008.*(w.^6)+0.0124.* (w.^5)+0.173.*(w.^4)+(w.^3)))./1+(((2.56.*(w.^2)+1.6.*w+1)./(0.0008.*(w.^6)+0.0124.*(w.^5)+0.173.*(w.^4)+(w.^3))))));
surf(T,w,sys2);
But I get this error:
??? Error using ==> surf at 78
X, Y, Z, and C cannot be complex.
What could be wrong please?
Or can anyone tell me how to plot this in Mathcad?
Thank you.
You can't plot a complex number versus two independent variables -- you would need four axes.
What you can do is:
Use two separate figures (or two subplots in the same figure) to plot real part and imaginary part. In Matlab,
surf(T,w,real(sys2));
figure %// create new figure for the other graph
surf(T,w,imag(sys2));
Alternatively, plot absolute value and phase:
surf(T,w,abs(sys2));
figure %// create new figure for the other graph
surf(T,w,angle(sys2));
A more exotic possibility is to use z axis for absolute value and colour for phase, in the same graph:
surf(T,w,abs(sys2),angle(sys2)); %// fourth argument of surf specifies colour

isosurface function MATLAB usage

Hi can any one give me a simple example of how to use the isosurface function in MATLAB.
The example given if you type help isosurface is quite confusing. Searching on google did not help as no one gives simple examples anywhere. All of them use predefined functions like
flow.
For starters, suppose i have points (x,y,z) where z=0 and at each point I define a constant
function f(x,y,z)=6. So if I use the isosurface function on the isovalue 6 I would like MATLAB to give me a 3d plot with the XY plane highlighted in some colour, say green.
I don't quite understand your example, but here's how you use isosurface to draw a sphere:
%# create coordinates
[xx,yy,zz] = meshgrid(-15:15,-15:15,-15:15);
%# calculate distance from center of the cube
rr = sqrt(xx.^2 + yy.^2 + zz.^2);
%# create the isosurface by thresholding at a iso-value of 10
isosurface(xx,yy,zz,rr,10);
%# make sure it will look like a sphere
axis equal
The example you gave is very uninteresting, in fact maybe even problematic.
By collapsing all points to z=0, you no longer can/need to use ISOSURFACE, and CONTOUR should be called instead. Even then, a constant function f(X,Y)=6 won't show anything either...
Since #Jonas already showed how to use ISOSURFACE, here is an example for the CONTOUR function:
%# create a function to apply to all X/Y coordinates
[X,Y] = meshgrid(-2:0.1:2,-1:0.1:1);
f = #(X,Y) X.^3 -2*Y.^2 -3*X;
%# plot the function surface
subplot(121), surfc(X,Y,f(X,Y))
axis equal, daspect([1 1 3])
%# plot the iso-contour corresponding to where f=-1
subplot(122), contour(X,Y,f(X,Y),[-1 -1]),
axis square, title('Contour where f(X,Y)=-1')