Specifying minor ticks on both yaxes - matlab

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;

Related

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')

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

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)]);

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.

Adding a second x-axis to my graph, which I can scale myself

I am having a problem with Matlab at the moment and I hope you can help me. I have written this code below to display a graph, but now I want to add another x-axis above the image, which I can scale myself. Is there any possibility to add another line like: set(gca, 'XTick2', [bla:bla:bla]); and another label?
EDIT: I have solved a part of the problem, hope you can help me with the rest, too... I have now 2x-axes, but still a few problems.
The labels are in wrong positions, the y-label is inside the scale and I want two different labels for the two x-axes.
Also I would like to remove the scale of the lower x-axis from the upper one.
Code is also the new one:
x1 = [0, 421.441, 842.882, 1264.323, 1685.764, 2107.205, 2528.646, 2950.087, 3371.528, 3792.969, 4214.41, 4635.851, 5057.29];
y1 = [55.659, 55.856, 56.081, 56.279, 56.312, 56.169, 56.038, 55.903, 55.75, 55.604, 55.512, 55.534, 55.661];
y2 = [51.231, 51.735, 52.063, 52.152, 51.632, 51.16, 51.014, 50.911, 50.721, 50.596, 50.597, 50.858, 51.242];
y3 = [50.939, 51.381, 51.644, 51.687, 51.353, 50.944, 50.829, 50.706, 50.538, 50.43, 50.412, 50.614, 50.948];
y4 = [50.023, 50.328, 50.506, 50.535, 50.352, 50.113, 50.032, 49.938, 49.801, 49.705, 49.672, 49.801, 50.03];
plot(x1,y1, 'ks-',x1,y2, 'bx--',x1,y3, 'gd-.',x1,y4, 'c.-'),
ax1 = gca;
ax1.XLim = [0, 5075];
ax1.XTick = 0:1000:5075;
ay1 = gca;
ay1.YLim = [49.5, 56.5];
ay1.YTick = 49.5:0.5:56.5;
ax2 = axes('Position',ax1.Position,'Color','none');
ax2.XLim = [0, 360];
ax2.XTick = 0:30:360;
ax2.XAxisLocation = 'top';
ax2.YTick = [];
grid off
xlabel('Time [s]')
ylabel('Temperature [°C]')
legend('A','B','C','D')
Just use the function text,
text(x,y,'string')
where x and y are the coordinates of you new x-axis and string your blabla.
UPDATE: use this sample code and adjust it to your needs
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;
figure
line(x1,y1,'Color','r')
ax1 = gca; % current axes
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
which gives the plot,

Matlab: y-label isn't shown in small figure window

I'm using the following code:
x = linspace(0, 9, 10);
y1 = x;
y2 = x.^2;
y3 = x.^4;
myfig = figure('Position', [500 500 400 320]); %[left, bottom, width, height]:
ax1 = gca;
hold on
p1 = plot(x,y1,'x--r');
p2 = plot(x,y2,'*-b');
xlabel('blaaa');
ylabel('fooo');
xlim([0 max(x)]);
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XTickLabel', [],...
'XColor','k','YColor','k');
ylabel(ax2, 'asdasd');
linkaxes([ax1 ax2],'x');
hold on
p3 = plot(x,y3,'s:g','Parent',ax2);
legend([p1 p2 p3], {'one', 'two', 'three'}, 'Location', 'NorthWest');
whereas the right y-label isnt shown: Is there a way to show it by decreasing either the margin between the left-edge and the left y-label or by decreasing the width of the actual plotted data without resizing the figure window?
Thanks
Edit: Added an image:
Either decrease the red margin or the 'green' size, because wherre the blue arrow is, the y-label is now shown anymore since it doesn't fit!
Try replacing the line:
ax1 = gca;
with:
ax1 = axes('Position',[0.11 0.11 0.75 0.812]);
To manually set the axis position/size in normalized units.