Shade area between two vertical lines [duplicate] - matlab

This question already has answers here:
MATLAB, Filling in the area between two sets of data, lines in one figure
(4 answers)
Closed 6 years ago.
I have data for 12 time points y1=[.61 .52 .45 .75 .76 .79 .82 .6 .66 .54 .43 .21]; I would like to plot this as a line plot and draw two vertical line at time point 7 and 8 and shade the area in between these two lines. This shaded area should be transparent enough to still show the line. I would also like to have a legend to show that shaded area = critical period or have "critical period" written within the area underneath the line. From this answer, I've tried:
y1=[.61 .52 .45 .75 .76 .79 .82 .6 .66 .54 .43 .21];
N=size(y1,2);
sky_blue = [0, 0, 1] ;
x=1:N;
plot([1:N]', y1, 'linewidth', 2.8, 'linestyle', '-', 'color', sky_blue);
hold on
x1=7;
x2=8;
y2=get(gca,'ylim');
plot([x1 x1],y2)
plot([x2 x2],y2)
h1 = fill(x1,x2,'b','EdgeColor','none');
The issue is that things are okay until the last line with which I can't get the shading between the two lines. Can anyone help?

You got it almost right! Simply change your last line of code to the following:
h1 = fill([x1 x1 x2 x2], [y2 fliplr(y2)], 'b','EdgeColor','none');
fill() function takes as input the x and y coordinates of the corners (vertices) of the region (polygon) to fill the color into in either the clockwise or the anti-clockwise fashion (polygon need not be closed (fill can close it for you)).
Here, we have passed in the vector arrays of the x and y coordinates of the four vertices of the polygon bounded by the two lines in the clockwise order starting from bottom left vertex. Note: fliplr() function just reverses the 1x2 column vector, y2 from left to right.

Related

Colormap and colorbar for 3d trajectory. (Matlab) [duplicate]

This question already has answers here:
Change color of 2D plot line depending on 3rd value
(5 answers)
Closed 6 months ago.
I'm looking to do something similar to the below image (source) in Matlab. Ignore the vectors and the shaded cone; I just want to
plot a trajectory on the unit sphere, given by a function m(x) (whose values are points on the sphere),
color it according to parameter x (running from -inf to +inf),
display a color bar which explains the mapping from x-value to color.
I can plot the sphere easily with the help of built-in functions sphere and mesh, and I can draw the trajectory with plot3. To generate the x-values I can use something like
x = tan((-1:0.01:1)*pi/2);
which gives a distribution of points fitting for the types of functions m(x) will be (most of the action will be around x = 0).
The tricky part is the color. I have looked at the colormap and colorbar built-ins, but both seem to pertain to meshes and surfaces rather than curves, so I don't see an obvious way to adapt it to my use case.
Does anyone have any ideas for how I might proceed?
When using plot it is not possible for one line to be multicoloured. In order to make one line multicoloured, you must plot each individual segment and then colour them individually. For example:
% make some dummy data
% note that this is in the form of 1 x N row vectors
t = -.99:0.01:0.99;
x = cos(t * pi/2);
y = sin(t * pi/2);
z = t;
% reorder data in order to plot individual line segments
indices = [1:length(x) - 1; 2:length(x)];
figure;
p = plot3(x(indices), y(indices), z(indices), 'LineWidth', 2);
set(p, {'Color'}, num2cell(parula(length(x) - 1), 2)); % see what happens when you comment out this line
You can change the colormap you want by changing the parula keyword above. You can use any of the supplied colormaps, or create your own (in the form of an M x 3 RGB triple matrix).
You can then add the colorbar (note that these are dummy values).
c = colorbar;
caxis([-6 6]);
set(c, 'Ticks', [-6, -4, -2, -1, 0, 1, 2, 4, 6]);
set(c, 'TickLabels', {'-\infty', '-4', '-2', '-1', '0', '1', '2', '4', '\infty'});
You can achieve a somewhat similar, but different, effect using scatter3(). It is similar to plot3() but will allow you to set color (and size, if you wish) for each data point that it plots. If the aesthetics suit you, it is a simple and versatile approach.

Matlab: Plot a line over an image plot

I want to plot a line plot on top of an image plot in Matlab
First I plot the image data
figure(1); clf;
imagesc(t); colorbar
hold on;
axis tight
and then the line plot
line(ysum,y,'Color','red')
hold off;
The line plot however deletes the image and sets the background to white.
How can I plot on top of the image?
Your code isn't wrong, but it is not a minimal reproducible example, since you haven't defined t, y, ysum. When you call imagesc(t) the rows and columns will be the indices of t. In other words, it is the same as calling imagesc([1, size(t,2)], [1, size(t,1)], t). If t is small (say 10 x 10) but the elements of y,ysum are large (e.g. > 1000) then the 10 x 10 image will still be there, but it will be squished into the corner. Almost invisible.
So you need to make sure that the range of y, ysum, t line up. A quick work-around:
xidx = [min(ysum), max(ysum)];
yidx = [min(y), max(y)];
imagesc(xidx, yidx, t);

Plotting circles with complex numbers in MATLAB

I want to make a figure in MATLAB as described in the following image
What I did is the following:
x = [1 2 3];
y = [2 2 4];
radius = [1 1.2 2.2];
theta = [-pi 0 pi];
figure;
scatter(x,y,radius)
How do I add an angle theta to the plot to represent a complex number z = radius.*exp(1j*theta) at every spacial coordinates?
Technically speaking, those are only circles if x and y axes are scaled equally. That is because scatter always plots circles, independently of the scales (and they remain circles if you zoom in nonuniformly. + you have the problem with the line, which should indicate the angle...
You can solve both issues by drawing the circles:
function plotCirc(x,y,r,theta)
% calculate "points" where you want to draw approximate a circle
ang = 0:0.01:2*pi+.01;
xp = r*cos(ang);
yp = r*sin(ang);
% calculate start and end point to indicate the angle (starting at math=0, i.e. right, horizontal)
xt = x + [0 r*sin(theta)];
yt = y + [0 r*cos(theta)];
% plot with color: b-blue
plot(x+xp,y+yp,'b', xt,yt,'b');
end
having this little function, you can call it to draw as many circles as you want
x = [1 2 3];
y = [2 2 4];
radius = [1 1.2 2.2];
theta = [-pi 0 pi];
figure
hold on
for i = 1:length(x)
plotCirc(x(i),y(i),radius(i),theta(i))
end
I went back over scatter again, and it looks like you can't get that directly from the function. Hopefully there's a clean built-in way to do this, and someone else will chime in with it, but as a backup plan, you can just add the lines yourself.
You'd want a number of lines that's the same as the length of your coordinate set, from the center point to the edge at the target angle, and fortunately 'line' does multiple lines if you feed it a matrix.
You could just tack this on to the end of your code to get the angled line:
x_lines = [x; x + radius.*cos(theta)];
y_lines = [y; y + radius.*sin(theta)];
line(x_lines, y_lines, 'Color', 'b')
I had to assign the color specifically, since otherwise 'line' makes each new line cycle through the default colors, but that also means you could easily change the line color to stand out more. There's also no center dot, but that'd just be a second scatter plot with tiny radius. Should plot most of what you're looking for, at least.
(My version of Matlab is old enough that scatter behaves differently, so I can only check the line part, but they have the right length and location.)
Edit: Other answer makes a good point on whether scatter is appropriate here. Probably better to draw the circle too.

MATLAB Fill area between two contour plots

I have two contour plots and I want to be able to fill from one contour in one image to the same height contour in the other.
In the plot you can see two lines of each color - these are the lines I want to fill between, with the same color as the lines (though preferably translucent). The code for these are as follows
test = repmat(repelem(0:6,2),10,1);
test1 = test(:,2:end-1);
test2 = test(:,1:end-2);
contour(test1,1:5);
hold on;
contour(test2,1:5);
I did think that maybe I could create another image that has the desired height at each bin and do some kind of contourf, but that could be a problem if in future the lines cross over, which they may well do. In that case, I'd like the area they cross to be a combination of the colors that are crossing over.
Have you try using ```fill``?
% test values
col = 'g';
x1=[6 6 6];y1=[1 5 10]; x2= [7 7 7];
x2 = [x1, fliplr(x2)];
inBetween = [y1, fliplr(y1)];
fill(x2, inBetween, col);

How to draw differently spaced grid lines in Matlab

On mathworks, I have found a code which is suppossed to draw grid lines in a plot:
g_x = -25:1.25:0;
g_y = -35:2.5:-5;
for i = 1:length(g_x)
plot([g_x(i) g_x(i)],[g_y(1) g_y(end)],'k:')% y grid lines
hold on
end
for i=1:length(g_y)
plot([g_x(1) g_x(end)],[g_y(i) g_y(i)],'k:') % x grid lines
hold on
end
here's a link
I don't understand the plot command: e.g. the y grid lines - one of the inputs is a vector containing all the spacing points of the x-axis, where I want to have a grid. These points are given in two columns and they are assigned to the second vector, which only contains the first and the last point shown on the y-axis. As I understand this command, it will for example take the first element g_x(1) and g_y(1) and plot a : , it will then take g_x(2) and g_y(1) and plot :, and so on. But How does it keep on plotting : from g_y(1) continuously until g-y(end) ?
To directly answer your question, it simply plots the two endpoints of each grid line and since the default LineStyle used by plot is a solid line, they will automatically be connected. What that code is doing is creating all permutations of endpoints and plotting those to form the grid.
Rather than creating custom plot objects (if you're using R2015b or later), you can simply use the minor grid lines and modify the locations of the minor tick marks of the axes.
g_x = -25:1.25:0;
g_y = -35:2.5:-5;
ax = axes('xlim', [-25 0], 'ylim', [-35 -5]);
% Turn on the minor grid lines
grid(ax, 'minor')
% Modify the location of the x and y minor tick marks
ax.XAxis.MinorTickValues = g_x;
ax.YAxis.MinorTickValues = g_y;
Basically the code does this:
g_x = -25:1.25:0;
generates a array with values -25.0000 -23.7500 -22.5000 -21.2500 ... 0
These are the positions where vertical lines are drawn
The same holds for g_y, but of course this determines where horizontal lines are drawn.
The option 'k' determined that it is a dashed line.
And the loops just loo through the arrays.
So in the first iteration the plot function draws a line from the position
[-25, -35]
to the position
[-25, -5]
So if you want to change the grid, just change the values stored in g_x
g_x = -25:3.0:0;
would for example draw vertical lines with the width 3.0
I hope this makes sense for you.