MATLAB creating chart with two x-axes: how to align them? - matlab

I'm trying to plot a function using two different x-axes. As you can see in the image, the axes are not overlapping properly!
How do I fix it? I've already set it up to have the same number of ticks on both axes but it didn't help!
This is the code I'm using:
ax1 = gca;
set(gca, 'box', 'off')
xlabel('Frequency shift, \Delta\omega (/THz)');
ylabel('Autocorrelation, F_{2}');
ylim([0.0,1]);
xlim([0, 10])
xlimits = get(ax1,'XLim');
ylimits = get(ax1,'YLim');
xinc = (xlimits(2)-xlimits(1))/5;
yinc = (ylimits(2)-ylimits(1))/5;
set(ax1,'XTick',[xlimits(1):xinc:xlimits(2)],...
'YTick',[ylimits(1):yinc:ylimits(2)])
ax1.XColor = 'b';
ax1.YColor = 'b';
ax1_pos = ax1.Position;
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
ax2 = gca;
xlim([0, 2.5]) %in nm (note two pi)
ylim([0.0,1]);
xlimits = get(ax2,'XLim');
ylimits = get(ax2,'YLim');
xinc = (xlimits(2)-xlimits(1))/5;
yinc = (ylimits(2)-ylimits(1))/5;
set(ax2,'XTick',[xlimits(1):xinc:xlimits(2)],...
'YTick',[ylimits(1):yinc:ylimits(2)])
xlabel('Wavelength shift, \Delta\omega(/nm)');
ylabel('Autocorrelation, F_{2}');
ax2 = gca;
%set(ax,'XMinorTick','on','YMinorTick','off')
ax2.FontSize = 15;
ax2.Box = 'on';
ax2.LineWidth = 2;
set(gca, 'box', 'off')
set(findall(gcf,'type','text'),'FontSize',15);

Try maybe
ax1_pos = get(gca,'Position');
ax2 = axes('Position', [ax1_pos(1) (ax1_pos(2) - 0.5) ax1_pos(3) ax1_pos(4)]);

Related

Specifying minor ticks on both yaxes

I have a plot with two different yaxes and I am trying to set MinorTickValues on both. I can turn them on on both axes, which works fine. However, when I specify the location of them they disappear on the other axis.
V = horzcat((-2:0.1:2), (2:-0.1:-2));
t = (0:0.01234568:1.01);
T = (0:25:2025);
figure(30);
yyaxis right
plot(t ,V, 'Linewidth',3); hold all;
xlabel('Time / s');ylabel('Voltage / V');
set(gca,'FontSize',fontsize, 'LineWidth',2,'TickLength',[0.025 0.025])
xticks([0., 0.25, 0.5, 0.75, 1]);
ylim([-2, 2]),
ax = gca;
ax.XAxis.MinorTick = 'on';
ax.XAxis.MinorTickValues = (0:0.05:1);
set(ax.YAxis, 'MinorTick', 'on')
set(ax.YAxis, 'MinorTickValues', (-2:0.25:2))
yyaxis left
top = plot(t ,T, 'Linewidth',3); hold all;
xlabel('Time / s');ylabel('Temperature / K');
ax = gca;
%ax.YAxis.MinorTick = 'on';
%ax.YAxis.MinorTickValues = (250:50:650);
set(ax.YAxis, 'MinorTick', 'on')
set(ax.YAxis, 'MinorTickValues', (250:50:650))
chH = get(gca,'Children');
set(gca,'Children',[chH(end);chH(1:end-1)]);
uistack(top, 'top');
What it looks like now:
When setting the MinorTickValues on the left axis, the MinorTicks on the right axis disappear and vice versa.
Any help is highly appreciated.
In the documentation of yyaxis, it says "An exception is that the YAxis property of the Axes object contains an array of two ruler objects (one for each y-axis)." So you should be able to change the ticks with ax.YAxis(1), ax.YAxis(2)
fontsize = 10;
V = horzcat((-2:0.1:2), (2:-0.1:-2));
t = (0:0.01234568:1.01);
T = (0:25:2025);
figure(30);
yyaxis right
plot(t ,V, 'Linewidth',3); hold all;
xlabel('Time / s');ylabel('Voltage / V');
set(gca,'FontSize',fontsize, 'LineWidth',2,'TickLength',[0.025 0.025])
xticks([0., 0.25, 0.5, 0.75, 1]);
ylim([-2, 2]),
yyaxis left
top = plot(t ,T, 'Linewidth',3); hold all;
xlabel('Time / s');ylabel('Temperature / K');
chH = get(gca,'Children');
set(gca,'Children',[chH(end);chH(1:end-1)]);
% uistack(top, 'top');
ax = gca;
ax.YAxis(2).MinorTick = 'on';
ax.YAxis(2).MinorTickValues = 0:0.05:1;
ax.YAxis(1).MinorTick = 'on';
ax.YAxis(1).MinorTickValues = 250:50:650;

Matlab subplot linkaxes is not lining up the axes as expected

I can't seem to get linkaxes to work (code is below). I am trying to get the subplots to line up, such that visually the x-axis is has the same range and width for both subplots. The misalignment is in both the pop-up figure window and the saved JPG. Using Matlab R2018a.
Here is my code, and below is what the figure looks like:
x1 = [27247 26973 27265 28924 27182 27430 26534 26839 7876 26484 29787 26934 27218 25777 27801 8250 34820 7980 26927 34639];
y1 = [-2350 -3334 -2948 -2336 -2778 -2813 -3383 -3635 -31 -3334 -4216 -3284 -2271 -2477 -2058 375 -821 351 -3441 -1108];
ax1 = subplot(2,1,1);
scatter(x1, y1)
box on
grid on
axis equal
xlims = get(gca, 'XLim')
ax = gca;
ax.XRuler.Exponent = 0;
ax.YRuler.Exponent = 0;
xlims = get(gca, 'XLim')
ax2 = subplot(2,1,2);
scatter(x1, y1)
xlim(xlims)
box on
grid on
ax = gca;
ax.XRuler.Exponent = 0;
linkaxes([ax1,ax2],'x')
I've also tried this (below), but it doesn't change the plot.
% adding this to the first subplot:
xlims = get(gca, 'XLim')
positioning = get(gca,'position');
% adding this to the second subplot:
xlim(xlims)
set(gca, 'position', [positioning(1) positioning(2)/5 positioning(3) positioning(4)]) %x y width height
And here is what the figure looks like:
This was solved on the Matlab forums, and here is a summary/application of the solution:
x1 = [27247 26973 27265 28924 27182 27430 26534 26839 7876 26484 29787 26934 27218 25777 27801 8250 34820 7980 26927 34639];
y1 = [-2350 -3334 -2948 -2336 -2778 -2813 -3383 -3635 -31 -3334 -4216 -3284 -2271 -2477 -2058 375 -821 351 -3441 -1108];
ax1 = subplot(2,1,1);
scatter(x1, y1)
box on
grid on
axis equal
xlims = get(gca, 'XLim')
ax = gca;
ax.XRuler.Exponent = 0;
ax.YRuler.Exponent = 0;
xlims = get(gca, 'XLim')
ax2 = subplot(2,1,2);
scatter(x1, y1)
xlim(xlims)
box on
grid on
ax = gca;
ax.XRuler.Exponent = 0;
set(gcf,'Resize','off') %%%%%%%% this line fixes it for some reason!
linkaxes([ax1,ax2],'x') % want to link x-axis only
%figOut = 'test';
%print(figOut, '-r300', '-djpeg')

How to decrease the size of the legend in a figure?

The following code plots a figure. The code should work on Matlab >= R2014b. I want to remove the space within the legend. How to do this?
x = 0:0.5:10;
figure; hold on;
plot(x,sin(x), 'Marker', 'o');
plot(x,cos(x), 'Marker', 's');
[leg, objs] = legend({'sin', 'cos'}, 'Location', 'SouthWest');
line_start_end = [0.01, 0.4];
line_text_step = 0.01;
% for each line, text object, adjust their position in legend
for i = 1:numel(objs)
if strcmp(get(objs(i), 'Type'), 'line')
% line object
if 2 == numel(get(objs(i), 'XData')) % line
set(objs(i), 'XData', line_start_end);
else % marker on line
set(objs(i), 'XData', sum(line_start_end)/2);
end
else
%text object
text_pos = get(objs(i), 'Position');
text_pos(1) = line_start_end(2) + line_text_step;
set(objs(i), 'Position', text_pos);
end
end
See the following result:
What I want is :
There is a submission named resizeLegend by David J. Mack on File Exchange that does exactly that.
You can replace your loop with just this line:
resizeLegend();
which gives:
As #Sardar Usama suggested, I put my solution here. I hope it is useful for you.
The first answer is nice. However, I finally use the following code to do this task.
close all
x = 0:0.5:10;
figure; hold on;
ph(1) = plot(x,sin(x), 'Marker', 'o');
ph(2) = plot(x,cos(x), 'Marker', 's');
ax = gca;
ax.Box = 'on';
bx = axes('position', [ax.Position(1:2), 0.3, 0.4], 'Box', 'on', 'XTick', [], 'YTick', []);%, 'Color', [0.8549,0.7020,1.0000]);
cx = axes('position', [ax.Position(1:2), 0.3, 0.4], 'Box', 'on', 'XTick', [], 'YTick', []);%, 'Color', [0.8549,0.5020,1.0000]);
[legb, objsb] = legend(bx, ph, {'sin', 'cos'}, 'Location', 'SouthWest');
[legc, objsc] = legend(cx, ph, {'sin', 'cos'}, 'Location', 'SouthWest');
line_start_end = [0.01, 0.3];
line_text_step = 0.05;
legendshrink(ax.Position(1:2), legb, objsb, bx, line_start_end, line_text_step);
legendshrink(ax.Position(1:2), legc, objsc, cx, line_start_end, line_text_step);
% you need only to adjust cx' position.
cx.Position(1:2) = [ax.Position(1)+ax.Position(3)-cx.Position(3), ax.Position(2)];
legc.Position(1:2) = cx.Position(1:2);
where legendshrink is defined as:
function legendshrink(leg_pos_xy, leg, objs, bx, line_start_end, line_text_step)
% leg_post_xy = ax.Position(1:2);
% [leg, objs] = legend(bx, line_handles, text_cell);
% line_start_end = [0.01, 0.4];
% line_text_step = 0.01;
% for each line, text object, adjust their position in legend
for i = 1:numel(objs)
if strcmp(get(objs(i), 'Type'), 'line')
% line object
if 2 == numel(get(objs(i), 'XData')) % line
set(objs(i), 'XData', line_start_end);
else % marker on line
set(objs(i), 'XData', sum(line_start_end)/2);
end
else
%text object
text_pos = get(objs(i), 'Position');
text_pos(1) = line_start_end(2) + line_text_step;
set(objs(i), 'Position', text_pos);
end
end
% get minimum possible width and height
legend_width_no_right = 0;
for i = 1:numel(objs)
% legend margin left
if strcmp(get(objs(i), 'Type'), 'line')
if numel(get(objs(i), 'XData')) == 2
leg_margin_x = get(objs(i), 'XData');
leg_margin_x = leg_margin_x(1)*leg.Position(3);
end
else
cur_right = get(objs(i), 'Extent');
cur_right = (cur_right(1)+cur_right(3))*leg.Position(3);
if cur_right > legend_width_no_right
legend_width_no_right = cur_right;
end
end
end
legend_width = legend_width_no_right + leg_margin_x;
legend_height = leg.Position(4);
bx.Position = [leg_pos_xy, legend_width, legend_height];
leg.Position(1:2) = bx.Position(1:2);
leg.Box = 'off';
end
resulting

Matlab: Fitting two x axis and a title in figure

I am having trouble getting my title to show when I have a figure with two x-axis.
The plot looks good and the axis scales are as I would like them to be but the second axis label and the title end up outside my figure.
How do I get the plot and axis to have the same size and change the size of the figure to include labels and title?
Here is a minimal example:
x1 = linspace(0, 5);
y11 = sin(x1);
y12 = cos(x1);
x2 = linspace(4, 12);
figure(1)
plot(x1, y11, 'r');
hold on
grid on
plot(x1, y12, 'k');
axis([0 5 -1 1.8]);
legend('sin(x)', 'cos(x)');
xlabel('x')
ylabel('y-label');
ax1 = gca;
ax1_pos = ax1.Position;
ax2 = axes('Position', ax1_pos,...
'XAxisLocation', 'top',...
'YAxisLocation', 'right',...
'Color', 'none');
ax2.YColor = 'w';
title('2:nd Harmonics');
line(x2,0,'Parent',ax2,'Color','k')
xlabel('n');
As a workaround you could pre-define the Position property (i.e. size) of the 1st axes before generating the plot so that the title appears correctly even if you add a 2nd axes. For example, right after the call to figure(1) add something like this:
ax1 = axes('Position',[0.11 0.11 0.75 0.75]);
Also, if you wish to print exponent values in the title you can use Latex formatting as follows:
title('2^{nd} Harmonics');
Here is the whole code with output:
clear
clc
close all
x1 = linspace(0, 5);
y11 = sin(x1);
y12 = cos(x1);
x2 = linspace(4, 12);
figure(1)
%// Set axes position manually
ax1 = axes('Position',[0.11 0.11 0.75 0.75]);
plot(x1, y11, 'r');
hold on
grid on
plot(x1, y12, 'k');
axis([0 5 -1 1.8]);
legend('sin(x)', 'cos(x)');
xlabel('x')
ylabel('y-label');
%ax1 = gca;
ax1_pos = get(ax1,'Position');
ax2 = axes('Position', ax1_pos,...
'XAxisLocation', 'top',...
'YAxisLocation', 'right',...
'Color', 'none');
set(ax2,'YColor','w');
%// Notice the Latex formatting to print the exponent
title('2^{nd} Harmonics');
line(x2,0,'Parent',ax2,'Color','k')
xlabel('n');
Then you can resize as you wish; the title stays visible.

Troubles adjusting ticks and gridlines on Matlab 2014b

I'm working on an assignment in which we need to plot curves with gridlines at specific points but I can't figure out how to do it.
This is what I got so far:
figure,
plot(x1,y1)
xlim([0 20]);
ylim([-0.5 1]);
ax = gca;
ax.XTickMode = 'manual';
ax.XTick = [0:5:20];
grid on;
And, on the picture below, is what I needed:
Well, you need to set the YTicks as well.
figure
x1 = 0:0.01:20;
y1 = exp(-x1/4).*sin(x1); plot(x1,y1);
plot(x1,y1)
xlim([0 20]);
ylim([-0.5 1]);
ax = gca;
ax.XTick = [0:5:20];
ax.YTick = [-0.4:0.2:1];
xlabel('$$f(x)$$','interpreter','latex')
ylabel('$$x$$','interpreter','latex')
title('$$f(x) = \exp{(-\frac{x}{4})} \cdot \sin(x)$$','interpreter','latex')
grid on;