Plotting a line above/below existing lines created with plotyy - matlab

I'm running this code:
t = linspace(0, 10, 1000);
y1 = 2*t;
y2 = 3*t;
figure;
[ax, h1, h2] = plotyy(t, y1, t, y2);
set(h1, 'LineWidth', 4);
set(h2, 'LineWidth', 4);
hold on;
h3 = plot([5, 5], [0, 3000], 'LineWidth', 6, 'Color', [0.6, 0.6, 0.6]);
Which creates this plot:
Notice how the vertical grey line appears on top of the blue line (y1) but below the green line (y2).
How do I plot the grey line either on top of the other two lines, or below the other two lines?

I see two options:
A. Bring the gray line forward by moving it to the second axes created by the plotyy command
set(h3,'parent',ax(2));
B. Place the gray line on the bottom by rearranging the order of the blue and grey lines on
the axes.
chld = [h1 h3];
set(ax(1),'children',chld); %# reorders the two lines so that the gray line is in back.

To make the gray line bottom, you can also change the order of drawing.
t = linspace(0, 10, 1000);
y1 = 2*t;
y2 = 3*t;
figure;
h3 = plot([5, 5], [0, max(y1)], 'LineWidth', 6, 'Color', [0.6, 0.6, 0.6]);
hold on;
[ax, h1, h2] = plotyy(t, y1, t, y2);
set(h1, 'LineWidth', 4);
set(h2, 'LineWidth', 4);
There's a trick in h3 = plot(...) to make sure the left scale is correct, though.

Related

grouped bar chart with 2 y-axes is displayed as stacked bar

I would like to plot the grouped bar chart for the comparison of three methods. I tried the following code and it is displayed as stacked bar chart.can you please help
dice =[0, 3, 5];
no_of_region=[42, 12, 5];
figure;
bar(dice',.2,'grouped','FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5)
ylabel('Dice Similarity index')
yyaxis right
bar(no_of_region, .2,'grouped','EdgeColor', 'r', 'LineWidth', 2);
legend('Dice Similarity Index','Number of regions')
legend('Location','northwest')
XTickLabel={'a' ; 'b';'c'};
XTick=[1 2 3]
set(gca, 'XTick',XTick);
set(gca, 'XTickLabel', XTickLabel);
set(gca, 'XTickLabelRotation', 45);
xlabel('Different Methods', 'Fontsize', 16)
ylabel('Number of Regions', 'Fontsize', 16)
title('Comparison of Algorithms', 'fontsize', 20);
set(gcf,'color','w');
OUTPUT I GOT AS:
One easy solution is to shift the bars left and right by half of their width (or more if you would like space between them). Here is the new code, which is also cleaned a bit stylistically:
% parameters
dice = [0, 3, 5];
no_of_region = [42, 12, 5];
X = [1, 2, 3]; % common x values for plot
bar_width = 0.2; % width of bars
% initialize figure
figure;
% left plot
bar(X.' - bar_width/2, dice', bar_width, 'grouped', ...
'FaceColor', [0 .5 .5], ...
'EdgeColor', [0 .9 .9], ...
'LineWidth', 1.5);
ylabel('Dice Similarity index');
% right plot
yyaxis right;
bar(X.' + bar_width/2, no_of_region, bar_width, 'grouped', ...
'EdgeColor', 'r', ...
'LineWidth', 2);
% plot formatting
legend({'Dice Similarity Index', 'Number of regions'}, ...
'Location', 'northwest');
XTickLabel = {'a' ; 'b'; 'c'};
XTick = X;
set(gca, ...
'XTick', XTick, ...
'XTickLabel', XTickLabel, ...
'XTickLabelRotation', 45);
xlabel('Different Methods', 'FontSize', 16);
ylabel('Number of Regions', 'FontSize', 16);
title('Comparison of Algorithms', 'FontSize', 20);
set(gcf, 'color', 'w');
Depending on desired behavior, you also might consider replacing figure; by clf;, which clears the existing figure or opens a new figure window if there is not already one open.

Change the legend of a Matlab figure

I would like to change the legend style of the following picture generated in Matlab:
x1=-5;
x2=5;
y1=-5;
y2=5;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
fill(x,y,'b')
legend('A')
As you can see the legend displays a blue rectangle. What I would like is a filled blue circle in place of the rectangle as if the picture was generated as a scatter plot. How can I obtain that?
I would suggest to add a fictive value with hold on; p = plot(NaN, NaN, 'b.', 'MarkerSize', 15); then legend this specific "fake" plot with: legend(p, 'A');
x1=-5;
x2=5;
y1=-5;
y2=5;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
fill(x,y,'b');
hold on; p = plot(NaN, NaN, 'b.', 'MarkerSize', 15);
legend(p, 'A')
#Bebs has a nice solution.
Another suggest is to change directly the legend icon:
[a,b] = legend('A');
b(2).Xdata = sin(-pi:0.1:pi)/10+0.4; % you can play with numbers to set size and location of circle
b(2).Ydata = cos(-pi:0.1:pi)/5+0.5;
Now you can set some other properties:
b(2).LineWidth = 1; % thicker line
b(2).FaceColor = [1 1 1]; % white fill
b(2).EdgeColor = [0 0 1]; % blue edge

How do I modify the legend in a Matlab plot?

I would like to draw four circles of two colors. I'm using circle function to draw a circle. I'm facing problem with legend(). It colors the two data with the same color.
function main
clear all
clc
circle([ 10, 0], 3, 'b')
circle([-10, 0], 3, 'b')
circle([ 10, 10], 3, 'r')
circle([-10, 10], 3, 'r')
% Nested function to draw a circle
function circle(center,radius, color)
axis([-20, 20, -20 20])
hold on;
angle = 0:0.1:2*pi;
grid on
x = center(1) + radius*cos(angle);
y = center(2) + radius*sin(angle);
plot(x,y, color, 'LineWidth', 2);
xlabel('x-axis');
ylabel('y-axis');
title('Est vs Tr')
legend('true','estimated');
end
end
The following picture shows the problem. Both colored as blue instead one of them is red.
Any suggestions?
You can make your function circle() return the plot handles. Store the handles in a vector. In the end, you only call legend() once, after plotting all circles. The first argument in legend are then the function handles which you want to appear in the legend. Something like this:
function main
% clear all % functions have their own workspace, this should always be empty anyway
clc
handles = NaN(1,2);
handles(1,1) = circle([ 10, 0], 3, 'b'); % handle of a blue circle
circle([-10, 0], 3, 'b')
handles(1,2) = circle([ 10, 10], 3, 'r'); % handle of a red circle
circle([-10, 10], 3, 'r')
% Nested function to draw a circle
function h = circle(center,radius, color) % now returns plot handle
axis([-20, 20, -20 20])
hold on;
angle = 0:0.1:2*pi;
grid on
x = center(1) + radius*cos(angle);
y = center(2) + radius*sin(angle);
h = plot(x,y, color, 'LineWidth', 2);
xlabel('x-axis');
ylabel('y-axis');
title('Est vs Tr')
end
% legend outside of the function
legend(handles, 'true','estimated'); % legend for a blue and a red circle handle
end
The result looks like this:
The thing is that you draw 4 things and only have 2 entries in the legend.
As such it will pick the color of the first four things to color the legend.
Not in the opportunity to try it myself now, but I guess that the easiest 'solution' would be to draw your third circle first and then the second one.
circle([ 10, 0], 3, 'b')
circle([ 10, 10], 3, 'r')
circle([-10, 0], 3, 'b')
circle([-10, 10], 3, 'r')

Plot second y axis using plot and fill (without plotyy)

Here is my code
clear all;clc
x = linspace(0, 10, 100);
axes('Position', [.075,.075,.9,.2], ...
'XColor', 'k', ...
'YColor', [0 0 0]);
fill(x, circshift(sin(x), 50), 'red', 'EdgeColor','None');
ylabel('Fancy Sine', 'FontSize', 11);
% Adding this the upper vanishes and the y axes is on left not right side
axes('Position', [.075,.075,.9,.2], ...
'XColor', 'k', ...
'YAxisLocation', 'Right', ...
'Color', 'none');
plot(x, x, 'Color', [.2 .4 .8]);
ylabel('Line Graph', 'FontSize', 11);
xlabel('X', 'FontSize', 11);
The single axis works fine
But when I want to add the second axis, the first disappears and the new axis is not on the right, but left side..
But both plots should be in one axis and the second y axis should be on the right side.
How to achieve this?
plot automatically sets the axis properties to the default. Use hold to stop this or specify the axis properties after your plot call.
An example of the former:
clear all;clc
x = linspace(0, 10, 100);
ax1 = axes('Position', [.075,.075,.9,.2], ...
'XColor', 'k', ...
'YColor', [0 0 0]);
p1 = fill(x, circshift(sin(x), 50), 'red', 'EdgeColor','None','Parent',ax1);
ylabel('Fancy Sine', 'FontSize', 11);
% Adding this the upper vanishes and the y axes is on left not right side
ax2 = axes('Position', [.075,.075,.9,.2], ...
'XColor', 'k', ...
'YAxisLocation', 'Right', ...
'Color', 'none');
hold on
p2 = plot(ax2,x, x, 'Color', [.2 .4 .8]);
hold off
ylabel('Line Graph', 'FontSize', 11);
xlabel('X', 'FontSize', 11);
Edit1: The reason you don't need to do this for your fill call is because it creates a patch object in your axes and does not invoke a plot command.

How to get arrows on axes in MATLAB plot?

I want to plot something like this:
x = 0:0.01:10;
f = #(x) 50* 1.6.^(-x-5);
g = #(x) 50* 1.6.^(+x-10);
plot(x, f(x));
hold on
plot(x, g(x));
I can't manage to get axes similar to the ones in this figure:
I know I can remove the top and right lines like in this question, but I don't know how to get the arrows on the edges.
I don't need the additional annotations, but I would like to remove the ticks on the axes. I know how to do this when the axes are "normal", but I'm not sure if it must be done in another way when the axes are already manipulated.
Does anyone know how to do this?
Well, don't say I didn't warn you :)
% Some bogus functions
f = #(x) 50* 1.6.^(-x-5);
g = #(x) 50* 1.6.^(+x-10);
% Point where they meet
xE = 2.5;
yE = f(xE);
% Plot the bogus functions
figure(1), clf, hold on
x = 0:0.2:5;
plot(x,f(x),'r', x,g(x),'b', 'linewidth', 2)
% get rid of standard axes decorations
set(gca, 'Xtick', [], 'Ytick', [], 'box', 'off')
% Fix the axes sizes
axis([0 5 0 5])
% the equilibrium point
plot(xE, yE, 'k.', 'markersize', 20)
% the dashed lines
line([xE 0; xE xE], [0 yE; yE yE], 'linestyle', '--', 'color', 'k')
% the arrows
xO = 0.2;
yO = 0.1;
patch(...
[5-xO -yO; 5-xO +yO; 5.0 0.0], ...
[yO 5-xO; -yO 5-xO; 0 5], 'k', 'clipping', 'off')
% the squishy wiggly line pointing to the "equilibrium" text
h = #(x)0.5*(x+0.2) + 0.1*sin((x+0.2)*14);
x = 2.7:0.01:3.5;
plot(x, h(x), 'k', 'linewidth', 2)
% the static texts
text(xE-yO, -0.2, 'Q^*', 'fontweight', 'bold')
text(-2*yO, yE, 'P^*', 'fontweight', 'bold')
text(-2*yO, 4, 'Price', 'rotation', 90, 'fontsize', 14)
text( 4, -0.2, 'Quantity', 'fontsize', 14)
text( .5, 4.2, 'Demand', 'fontsize', 14, 'rotation', -55)
text( 4.0, 3.3, 'Supply', 'fontsize', 14, 'rotation', +55)
text( 3.6, 2.1, 'Equilibrium', 'fontsize', 14)
Result:
The symbolic math toolbox has provisions for making these arrows, but without that toolbox you are stuck with drawing the arrows yourself. The following code should be useful for this purpose:
% determine position of the axes
axp = get(gca,'Position');
% determine startpoint and endpoint for the arrows
xs=axp(1);
xe=axp(1)+axp(3)+0.04;
ys=axp(2);
ye=axp(2)+axp(4)+0.05;
% make the arrows
annotation('arrow', [xs xe],[ys ys]);
annotation('arrow', [xs xs],[ys ye]);
% remove old box and axes
box off
set(gca,'YTick',[])
set(gca,'XTick',[])
set(gca,'YColor',get(gca,'Color'))
set(gca,'XColor',get(gca,'Color'))
The only drawback is that for some figure window sizes you will have a 1-pixel white border below the arrows, and setting the LineWidth property of the axes to a ridiculous small value does not help.
But for printing, the small white border should not be relevant.