how to get vertical gradient fill in matlab plot - matlab

I have a function y=x^2 to plot and I would like to fill the area bound by this curve with gradient color from black to red along the y direction. I found the color online
x = linspace(-3, 3, 20)';
f = x.^2;
M = f.^2;
N = length(x);
verts = [x(:), f(:)-max(f); x(:) zeros(N,1)];
q = (1:N-1)';
faces = [q, q+1, q+N+1, q+N];
p = patch('Faces', faces, 'Vertices', verts, 'FaceVertexCData', [M(:); M(:)], 'FaceColor', 'interp', 'EdgeColor', 'none')
This code show me the area bound with the curve to the xaxis, like
But what I want to fill is the WHITE area. So I modify the code as follows
x = linspace(-3, 3, 20)';
f = x.^2;
M = f.^2;
N = length(x);
verts = [x(:), f(:)-max(f); x(:) zeros(N,1)];
q = (1:N-1)';
faces = [q, q+1, q+N+1, q+N];
p = patch('Faces', faces, 'Vertices', verts, 'FaceVertexCData', [M(:); M(:)], 'FaceColor', 'interp', 'EdgeColor', 'none')
this gives me something like the following
it fill the right area but two issues:
the whole image shifted to negative y axis by 9 units
the gradient color was fill from left to right but I want it to fill from bottom to top (vertically)
the colormap was not defined as black to red (I can change that manually though)

The vertical shift happened because of the line
verts = [x(:), f(:)-max(f); x(:) zeros(N,1)];
where f(:) - max(f) is always <=0 . I think you just need to use f(:) to solve that problem.
As for the direction of the shading, since you are defining your patches as vertical strips, they will be colored as vertical strips. If you want horizontal shading, you need to define horizontal patches. Can you figure that out, or do you need help?
EDIT - this code does what you are asking:
figure;
x=linspace(-3, 3, 200);
f = x.^2;
plot(x, f, 'r'); hold on % you could leave this line out... then there is no curve
N = numel(x);
for ii = ceil(N/2):N-1
ix = [ii ii+1 N-ii N-ii+1];
disp(ix)
patch(x(ix), f(ix), f(ii)*256/max(f(:)),'edgecolor', 'none');
end
Here is the output:
One more problem - you are defining the color M as f.^2 - isn't that one round of squaring too many? I think you meant it to be x.^2 (or just F)?

There is a possibly much easier way to achieve almost the same thing... draw the picture as follows:
cplot = repmat(1:256, [256 1])'; % create a 256 square horizontal gradient
xv = linspace(-3, 3, 256); % range of x values
yv = linspace(0, 9, 256); % range of y values
cplot(repmat(xv.^2, [256 1])>repmat(yv', [1 256]))=255; % set area below the curve to white
figure
imagesc(xv, yv, cplot);
Now all you have to do is fix the color scale... you will want the color 255 to be white...

Related

How to find the area of an arbitrary shape contained within a circle using MATLAB

I have an arbitrary shape, of which the exterior boundary has been traced in MATLAB using bwboundaries. Using regionprops, I can calculate the total area enclosed by this shape.
However, I want to know the area for only the parts of the shape that fall within a circle of known radius R centered at coordinates [x1, y1]. What is the best way to accomplish this?
There are a few ways to approach this. One way you could alter the mask before performing bwboundaries (or regionprops) so that it only includes pixels which are within the given circle.
This example assumes that you already have a logical matrix M that you pass to bwboundaries.
function [A, boundaries] = traceWithinCircle(M, x1, y1, R);
%// Get pixel centers
[x,y] = meshgrid(1:size(M, 1), 1:size(M, 2));
%// Compute their distance from x1, y1
distances = sqrt(sum(bsxfun(#minus, [x(:), y(:)], [x1, y1]).^2, 2));
%// Determine which are inside of the circle with radius R
isInside = distances <= R;
%// Set the values outside of this circle in M to zero
%// This will ensure that they are not detected in bwboundaries
M(~isInside) = 0;
%// Now perform bwboundaries on things that are
%// inside the circle AND were 1 in M
boundaries = bwboundaries(M);
%// You can, however, get the area by simply counting the number of 1s in M
A = sum(M(:));
%// Of if you really want to use regionprops on M
%// props = regionprops(M);
%// otherArea = sum([props.Area]);
end
And as an example
%// Load some example data
data = load('mri');
M = data.D(:,:,12) > 60;
%// Trace the boundaries using the method described above
B = traceWithinCircle(M, 70, 90, 50);
%// Display the results
figure;
hax = axes();
him = imagesc(M, 'Parent', hax);
hold(hax, 'on');
colormap gray
axis(hax, 'image');
%// Plot the reference circle
t = linspace(0, 2*pi, 100);
plot(x1 + cos(t)*R, y1 + sin(t)*R);
%// Plot the segmented boundaries
B = bwboundaries(M);
for k = 1:numel(B)
plot(B{k}(:,2), B{k}(:,1), 'r');
end

MATLAB: How do I shade plot area with increasing and decreasing shades of a color

I want to plot an area of a graph (x = 1:24, y = -1:1) with one color (black), which I then want to decrease/increase in shading in terms of the time of day. So, that I have a plot which is 'dark' in the background at night and 'light' during the day with x being hours of day and y being a data value. My sunrise would be at 6.8 and my sunset would be at 22. I would then overlay scatter plots with data on top.
I have tried messing around with patch and area but with no luck. Here is some code I got from elsewhere on the internet but I'm not sure how to proceed:
% Define x, f(x), and M(x)
x = linspace(6.8, 22)';
f = sin(x)+cos(x);
M = x.^2;
% Define the vertices: the points at (x, f(x)) and (x, 0)
N = length(x);
verts = [x(:), f(:), x(:) zeros(N,1)];
% Define the faces to connect each adjacent f(x) and the corresponding points at y = 0.
q = (1:N-1)';
faces = [q, q+1, q+N+1, q+N];
p = patch('Faces', faces, 'Vertices', verts, 'FaceVertexCData', [M(:); M(:)], 'FaceColor', 'interp', 'EdgeColor', 'none')
So I want the end result to be similar to the image attached below (note faint shading - I would like the gradient stronger), but with x = 1:24 and y = -1:1.
Since you ignored my request to include the results of your MATLAB code in your question, I had to look into my crystal ball to determine that your "messing around" resulted in the following error message:
Error using patch
While setting the 'Vertices' property of Patch:
Value must be a 1x2 or 1x3 vector of numeric type
Error in X (line 13)
p = patch('Faces', faces, 'Vertices', verts, 'FaceVertexCData', [M(:); M(:)], 'FaceColor', 'interp', 'EdgeColor', 'none')
This can be resolved by concatenating the vertices vertically instead of horizontally in line 8:
verts = [x(:), f(:); x(:), zeros(N,1)];
This yields the following plot:
This is of course not what you want.
Actual Solution
You can build up your background from 5 rectangles:
x = [0;
6; % 'First daylight'
6.8; % Full brightness
22; % Beginning of 'sunset'
22.8; % Complete darkness
24];
vertices = [repmat(x,2,1), [ones(size(x)); -1.*ones(size(x))]];
faces = [1:5; 2:6; 8:12; 7:11].';
color = repmat([0 0 0; % 'Night' color
0 0 0;
1 1 1; % 'Day' color
1 1 1;
0 0 0;
0 0 0],2,1);
patch('Faces',faces, ...
'Vertices',vertices, ...
'FaceVertexCData',color, ...
'FaceColor','interp', ...
'EdgeColor','red');
xlim([0 24]);
You make the vertices of the 'night' rectangles black and the vertices of the 'day' rectangle white by assigning the appropriate RGB values to the FaceVertexCData property.
The color of 'sunrise'/'sunset' rectangles is then interpolated between black and white by setting the FaceColor property to 'interp'.
The EdgeColor has only been set to 'red' to illustrate how the background is constructed. Set the property to 'interp' to make the lines disappear.

How to plot spheres on top of scattered stars in MATLAB?

I would like to make a scattered stars and plot multiple spheres on top of it? I tried doing this:
x = rand(100,1); y = rand(100,1); z = rand(100,1);
scatter(x,y,z,'c*')
After this I tried plotting sphere but the sphere pushes the stars to the side. How do I fix this?
vec = [1;1;1]; rads = 1;
[x y z] = sphere;
x = rads*x+vec(1); y = rads*y+vec(2);
z = rads*z+vec(3);
surf(x, y, z, 'Edgecolor', 'none')
colormap colorcube
As you can see it pushed the stars to the side
Thank you.
Its pushing the scatter plot to the side because the range of the 'stars' is [0 1] because the command rand(...) generates values between 0 and 1. On the other hand, the sphere goes from 0 to 2 in all 3 directions.
To fix the problem, you can simply multiply the data used to generate the scatter plot by 2, so they will be in the range [0 2].
Doing so results in the following:
And the code. Note that I used scatter3 instead of scatter.
clear
clc
close all
xs = 2*rand(100,1); ys = 2*rand(100,1); zs = 2*rand(100,1);
hScatter = scatter3(xs,ys,zs,'c*')
hold on
vec = [1;1;1]; rads = 1;
[x y z] = sphere;
x = rads*x+vec(1); y = rads*y+vec(2);
z = rads*z+vec(3);
surf(x, y, z, 'Edgecolor', 'none')
colormap colorcube
rotate3d on

Matlab rejection sampling points not displayed

I am using the following code for Pi approximation, using the rejection sampling method.
% DISPLAY A CIRCLE INSCRIBED IN A SQUARE
figure;
a = 0:.01:2*pi;
x = cos(a); y = sin(a);
hold on
plot(x,y,'k','Linewidth',2)
t = text(0.5, 0.05,'r');
l = line([0 1],[0 0],'Linewidth',2);
axis equal
box on
xlim([-1 1])
ylim([-1 1])
title('Unit Circle Inscribed in a Square')
pause;
rand('seed',12345)
randn('seed',12345)
delete(l); delete(t);
% DRAW SAMPLES FROM PROPOSAL DISTRIBUTION
samples = 2*rand(2,100000) - 1;
% REJECTION
reject = sum(samples.^2) > 1;
% DISPLAY REJECTION CRITERION
scatter(samples(1,~reject),samples(2,~reject),'b.')
scatter(samples(1,reject),samples(2,reject),'rx')
hold off
xlim([-1 1])
ylim([-1 1])
The expected result should be a blue circle inside of a red square. When I run the code the red point for building the square are displayed, but the blue point not.
The expected result should be as the picture found at this Link
But I get the following result:
Does anybody know what I can possibly do in order to visualize the blue points as well? Thanks in advance.
The scatter function is probably not the best choice for such huge number of points. Try this instead:
t = 0:.01:2*pi;
x = cos(t); y = sin(t);
samples = 2*rand(2,100000) - 1;
reject = sum(samples.^2) > 1;
props = {'LineStyle','none', 'Marker','.', 'MarkerSize',1};
line(x, y, 'Color','k', 'LineWidth',2)
line(samples(1,~reject), samples(2,~reject), props{:}, 'Color','b')
line(samples(1,reject), samples(2,reject), props{:}, 'Color','r')
axis equal; axis([-1 1 -1 1])
box on

Adding space between cells in Matlab imagesc output

I am creating a 2D plot in Matlab by calling this command: imagesc(vector1, vector2, mat_weights). Then, I run the colorbar command.
I now have a smooth 2D plot, but I want to add space between the cells. Here's how I want it to look:
How do I add such spacing between the cells/boxes?
You can add spaces between patches of color using another function than imagesc. Here, scatter provides a straightforward solution when used with option 'filled' and marker 'square'.
Note that you need to transform your 2-D matrix into a vector, but you don't have to scale your data: scatter takes the min and max values from your data and assign them to the min and max colors of the colormap.
The code
% 2-D in 1-D:
Z = diag(1:10); %example of 2-D matrix to be plotted
C = reshape(Z,1,[]); %1-D transform for vector color
% input definition
sz_matrix = 10;
X = repmat( (1:sz_matrix), 1, sz_matrix);
Y = kron(1:sz_matrix,ones(1,sz_matrix));
S = 1000; % size of marker (handle spaces between patches)
%C = (X.^2 + Y.^2); % second color scheme
%plot
figure('Color', 'w', 'position', [10 10 600 400]);
scatter(X, Y, S, C, 'fill', 's');
set(gca, 'XLim', [0 11], 'YLim', [0 11]);
axis square;
colormap summer
colorbar
will give
EDIT
Here is a piece of code for a rectangular matrix. Please note the inversion of the Y axis direction so that the graphical representation matches disp(Z). To have similar (x,y) proportion in the white area separating color patches, one may try to resize manually the figure.
Z = diag(1:10); %example of 2-D matrix to be plotted
Z = Z(1:end-2,:); %trim for rectangular
% input definition
X = repmat(1:size(Z,2), 1, size(Z,1));
Y = kron(1:size(Z,1),ones(1,size(Z,2)));
C = reshape(Z',1,[]); %1-D transform for vector color
S = 1000; % size of marker (handle spaces between patches)
%plot
figure('Color', 'w');
scatter(X, Y, S, C, 'fill', 's');
set(gca, 'XLim', [0 size(Z,2)+1], 'YLim', [0 size(Z,1)+1]);
colormap jet
colorbar
set(gca, 'YDir','reverse');
The ouput: