Related
I want to create a graphic representation. I have a sphere with radius of 50. I need to create two different filled capacities when it is one-fourth and three-fourths of its total capacity.
What I already have is this:
[x,y,z] = sphere();
r = 50;
surf( r*x, r*y, r*z ) % sphere with radius 50 centred at (0,0,0)
You could create the spere in two parts. Take a look at the following example:
% First part -- 0 to pi/2
theta = linspace(0,pi/2);
phi = linspace(-pi/2,pi/2);
[theta, phi] = meshgrid(theta, phi);
rho = 50;
[x, y, z] = sph2cart(theta, phi, rho);
surf(x,y,z, 'EdgeColor', 'b');
% Second part -- 90 to 360
hold on;
theta = linspace(pi/2,2*pi);
[theta, phi] = meshgrid(theta, phi);
[x, y, z] = sph2cart(theta, phi, rho);
surf(x,y,z, 'EdgeColor', 'r');
hold off;
It produces a graph like the following.
I have a circular lattice and on the lattice sites I plot normalized arrows that remain in the same magnitude and change direction according to a simulation, the details of which don't matter.
My plots look like this
Is it possible to replace the arrow in the quiver plot by an jpg/bmp/gif/png image? or by any other command?
Ideally, it would look something like this (although not necessarily arrows)
Explanation
One way that you can do this, would be to use a surface object with a texture-map as the FaceColor.
In MATLAB, you can create a simple rectangular surface. You can set the FaceColor to be texturemap which will cause the value assigned to CData to be mapped across the surface.
Then to get transparency, you can also set the FaceAlpha value to be texturemap and set the AlphaData and those transparency values will be mapped across the extent of the surface as well.
For this to be applied to your case, you want to set the CData to the image that you want to use to replace your arrows. And you will want the AlphaData to be the same size as your image data with values of 1 where you want it to be opaque and 0 where you want it to be transparent. This will allow it to not look like the image that you have posted where you can clearly see the bounding box. Then you will need to draw one of these surfaces where each of the arrows would go and scale/position it appropriately.
Implementation
Update: A more polished version of this code (ImageQuiver) is now available on Github as well as the MATLAB File Exchange.
As a demonstration of what I'm talking about, I have created the following function which essentially does just this. It accepts the same inputs as quiver (with the image data being supplied first and an optional AlphaData parameter at the end) and creates a surface at all of the requested coordinates pointing in the requested direction, and scaled by the specified amount.
function h = quiverpic(im, X, Y, dX, dY, scale, alpha)
% im - RGB or indexed image
% X - X positions
% Y - Y positions
% dX - X direction vector
% dY - Y direction vector
% scale - Any scaling (Default = 1)
% alpha - Transparency (same size as im), if not specified = ~isnan(im)
h = hggroup();
if ~exist('scale', 'var')
% By default there is no scaling
scale = 1;
end
if ~exist('alpha', 'var')
% By default, any NaN will be transparent
alpha = ~isnan(im);
end
% Determine aspect ratio of the source image
width_to_height = size(im, 2) / size(im, 1);
for k = 1:numel(X)
% Determine angle from displacement vectors
theta = atan2(dY(k), dX(k));
% Subtract pi/2 to +y is considered "up"
theta = theta + pi/2;
% Setup surface plot boundary
[xx,yy] = meshgrid([-0.5, 0.5] * width_to_height, [0 1]);
% Scale depending on magnitude of dX and dY
this_scale = scale * sqrt(dX(k).^2 + dY(k).^2);
% Scale X and Y components prior to rotating
xx = xx .* this_scale;
yy = yy .* this_scale;
% Rotate to align with the desired direction
xdata = xx .* cos(theta) - yy .* sin(theta);
ydata = xx .* sin(theta) + yy .* cos(theta);
% Determine what is considered the "anchor" of the graphic.
% For now this is assumed to be the "bottom-middle"
xoffset = X(k) - mean(xdata(2,:));
yoffset = Y(k) - mean(ydata(2,:));
% Actually plot the surface.
surf(xdata + xoffset, ...
ydata + yoffset, zeros(2), ...
'Parent', h, ...
'FaceColor', 'texture', ...
'EdgeColor', 'none', ...
'CData', im, ...
'FaceAlpha', 'texture', ...
'AlphaData', double(alpha));
end
end
Example
I wrote a little test script to show how this can be used and to show the results.
t = linspace(0, 2*pi, 13);
dX = cos(t(1:end-1));
dY = sin(t(1:end-1));
X = (3 * dX) + 5;
Y = (3 * dY) + 5;
scale = 1;
% Load the MATLAB logo as an example image
png = fullfile(matlabroot,'/toolbox/matlab/icons/matlabicon.gif');
[im, map] = imread(png);
im = ind2rgb(im, map);
% Determine alpha channel based on upper left hand corner pixel
flatim = reshape(im, [], 3);
alpha = ~ismember(flatim, squeeze(im(1,1,:)).', 'rows');
alpha = reshape(alpha, size(im(:,:,1)));
% Plot some things prior to creating the quiverpic object
fig = figure();
hax = axes('Parent', fig);
axis(hax, 'equal');
% Plot a full circle
t = linspace(0, 2*pi, 100);
plot((cos(t) * 3) + 5, (sin(t) * 3) + 5, '-')
hold(hax, 'on')
% Plot markers at all the quiver centers
plot(X, Y, 'o', 'MarkerFaceColor', 'w')
% Plot a random image behind everything to demonstrate transparency
him = imagesc(rand(9));
uistack(him, 'bottom')
axis(hax, 'equal')
colormap(fig, 'gray')
set(hax, 'clim', [-4 4]);
% Now plot the quiverpic
h = quiverpic(im, X, Y, dX, dY, 1, alpha);
axis(hax, 'tight')
Results
Absurdity
Same image with varying vectors and scaling
Any image of any aspect ratio will work just fine
In the code below, I am attempting to move a ball from 90 degrees to 44 degrees and back to 90 degrees. I wanted to be able to track the position of the ball's center point. I am trying to do this by plotting a marker that follows the ball. I would also like to keep seeing the (x,y) position of the ball being updated as the ball moves.
Can you help me modify my code so that:
1. The marker remains in the center of the ball, and
2. Keep displaying the (x,y) position of the marker (ball's center) as it moves?
Here is what I have done so far:
close all; clc; clear;
% Define position of arm
theta=0:10:360; %theta is spaced around a circle (0 to 360).
r=0.04; %The radius of our circle.
%Define a circular magenta patch.
x=r*cosd(theta) + 0.075;
y=r*sind(theta) + 1;
% Size figure and draw arms
figure('position', [800, 300, 600, 550]);
point = plot(0.075,1, 'bo', 'markers', 3);
hold on
myShape2=patch(x,y,'m');
set(myShape2,'Xdata',x,'Ydata',y);
axis([-1.5 3 -1 3]); grid on;
n = 1;
T=0.15; %Delay between images
for theta = pi/2:-pi/90:0,
if theta >= pi/4;
theta = theta;
else
theta = pi/2 - theta;
end
Arot = [sin(theta) cos(theta); -cos(theta) sin(theta)];
xyRot = Arot * [x; y]; % rotates the points by theta
xyTrans = xyRot;
point = plot(xyTrans(1, n),xyTrans(2, n), 'bo', 'markers', 3);
hold on;
set(myShape2,'Xdata',(xyTrans(1, :)),'Ydata',(xyTrans(2, :)));
pause(T); %Wait T seconds
end
Thanks for your time and help!
close all; clc; clear;
% Define position of arm
theta=0:10:360; %theta is spaced around a circle (0 to 360).
r=0.04; %The radius of our circle.
%Define a circular magenta patch.
x=r*cosd(theta) + 0.075;
y=r*sind(theta) + 1;
% Size figure and draw arms
figure('position', [800, 300, 600, 550]);
hold on
myShape2=patch(x,y,'m');
set(myShape2,'Xdata',x,'Ydata',y);
point = plot(0.075,1, 'bo', 'markers', 1);
dim = [.2 .5 .3 .3];
axis([-1.5 3 -1 3]); grid on;
T=0.15;
% Create annotation object to display object center
ano = annotation('textbox',dim,'String',['center position: (','0.075, 1'],'FitBoxToText','on');
for theta = pi/2:-pi/90:0,
if theta >= pi/4;
theta = theta;
else
theta = pi/2 - theta;
end
Arot = [sin(theta) cos(theta); -cos(theta) sin(theta)];
xyTrans = Arot * [x; y];
% Remove plot and annotation object from previous frame
delete(point)
delete(ano)
set(myShape2,'Xdata',(xyTrans(1, :)),'Ydata',(xyTrans(2, :)));
% Calculate the center of the patch as mean x and y position
x_pos = mean(xyTrans(1, :));
y_pos = mean(xyTrans(2, :));
point = plot(x_pos, y_pos, 'bo', 'markers', 1);
ano = annotation('textbox',dim,'String',['center position: (',sprintf('%0.3f', x_pos),', ',...
sprintf('%0.3f', y_pos),')'],'FitBoxToText','on');
axis([-1.5 3 -1 3]); grid on;
pause(T);
end
The code is:
subplot(1,3,3)
h=surf(ReflMatrix)
set(h, 'edgecolor','none')
colormap winter %Other colourmaps: Winter,Cool
hold on;
ylabel('frequency (Hz)');
xlabel('angle of incidence (degrees)');
alpha(.5) %transparency
The ReflMatrix is 401x90. The values of y range from 0 to 90, which is good because y is angle measured in degrees . The values of x (frequency) range from 0 to 401 because my bandwidth is 401 frequencies but I would like the same graph with values ranging from 300 to 700 (instead of starting from frequency 0 to start from frequency 300).
In surf you can specify your x and y. In your case, define your frequency by
y = linspace(300,700,401);
and the phase by
x = linspace(0,90,91);
Are you sure with the size of ReflMatrix, since frequencies from 0 to 90 are 91 points rather than 90. Then set your x and y parameters according to
[X,Y] = meshgrid(x,y);
h = surf(X,Y,ReflMatrix);
EDIT:
You can set the limits of the axes accordingly by
xlim([0 90]);
ylim([300 700]);
zlim([min(min(ReflMatrix)) max(max(ReflMatrix))]);
I can create a plot with pol2cart but how can I get the angles to show up at the end off the lines? See code below:
hold on ;
for angle = 0:20:(360-20)
[x1,y1] = pol2cart( angle / 180 * pi , [0 2]);
plot(x1,y1,'r')
end
for rho = 0:0.1:2
[x1,y1] = pol2cart( 0:0.01:2*pi , rho);
plot(x1,y1,'b')
end
axis equal
I'm trying to get the angle increments to show up all around see image below.
Please note I didn't draw all the numbered angles just the first couple to show what I'm trying to do
PS: I'm using octave 3.8.1 which tries to use the same language syntax as matlab
You could do something like
step = 20;
r= 2.2;
for idx = 0:step:360-step
text(r*cos(pi*idx/180),r*sin(pi*idx/180),num2str(idx), ...
'HorizontalAlignment','center', 'color',[1 .5 0])
end
For a better fit, include
axis(1.05*[-r r -r r])
axis equal
(This works for MATLAB, I dont know if the syntax is exactly the same in Octave.)
Here's the information and plot animated
%polar_chart_with_angles and there opposites
clear all, clc, clf
%find how many angles to make one full cycleremeber to divide by two if using stereo signal 180 out of phase
incr=72;
angle_wanted=incr;
n = lcm(360, 180 - angle_wanted) / (180 - angle_wanted)
angle_div=[0:incr:incr*n] %angle divsions
angle_div_mod=mod(angle_div,360) %angle divsions mod into 360
angle_div_mod_opp=mod(angle_div+180,360) %oppsite angle divsions mod into 360
%for circles
r= 2.2;
for rho = 0:0.1:2
[x1,y1] = pol2cart( 0:0.01:2*pi , rho);
plot(x1,y1,'b')
axis(1.10*[-r r -r r])
axis equal
hold on;
end
%for orig angles
for ii=1:n
angle=angle_div(ii)
[x1,y1] = pol2cart( angle / 180 * pi , [0 2]);
plot(x1,y1,'r')
hold on;
title_h=title(['Norig= ', int2str(ii)]);
%title_h = title('This is the title');
set(title_h, 'Position', [0.5, 0.02],'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'left')
%%for creating orig angles
idx=angle_div_mod(ii);
text(r*cos(pi*idx/180),r*sin(pi*idx/180),num2str(idx), 'HorizontalAlignment','center', 'color',[1 .5 0])
pause (.1)
end
%for oppsite angles
for ii=1:n
angle_opp=angle_div_mod_opp(ii)
[x1,y1] = pol2cart( angle_opp/ 180 * pi , [0 2]);
plot(x1,y1,'g')
hold on;
title(['Nopp= ', int2str(ii)]);
%for creating oppsite angles
idx=angle_div_mod_opp(ii);
text(r*cos(pi*idx/180),r*sin(pi*idx/180),num2str(idx), 'HorizontalAlignment','center', 'color',[.5 .7 .7])
pause (.1)
end