Double x and y axis zoom and secondary y axis numerical ticks aligned right

8 visualizaciones (últimos 30 días)
Hi, I'm trying to recreate the plot/diagram below. Though it would be a good exercise in using the secondary x axis, but I've ran into a couple of issues that I can't for the life of me figure out.
I've got it working pretty ok, happy to have the and USGPM rotated. I did play around with the YLabel.Position option, but found it clumsy.
So, I've three questions;
  1. Is there a way to have the numbers on the secondary y axis be aligned right, so it is not too crowded in the bottom right corner.
  2. When I open the output figure from the livescript into its own figure windows, when I zoom, it zooms on the secondary x and y axis, not on all axecs. I've tried linkaxes but that seem to be for just subplots as it just normalises the axis.
  3. Issue with title. When I try and give the plot/figure a title, it works and looks fine when I see the actual figure, like below in the code. When I do the conversion to a pdf, then the pdf has the title superimposed on top of the secondary x axis label in th eimage below. When I use title for the secondary x axes, it works perfectly, just like I want. Seems like the secondary axes and the figure title are occuplying the same space. Not the biggest issue, but just courious why this is happening.
Though this would be a nice straight forward potting exervcise, but it has taken on a life of its own, but I'm learning heaps.
Thankls in advance
Gerard
% Axes values
x1 = 0:600:6600;
y1 = [0:20:380,396];
x2 = x1/60;
y2 = round(y1 * 4.40288);
% h = figure;
t = tiledlayout(1,1);
title(t,'Conversion of Flow Rates')
% Primary Axes
ax1 = axes(t);
ax1.XLim = ([min(x1) max(x1)]);
ax1.XTick=(x1);
ax1.XTickLabelRotation = 90;
ax1.XLabel.String = 'L/min';
ax1.YLim = ([min(y1) max(y1)]);
ax1.YTick = (y1);
ax1.YLabel.String = 'm^3/h';
ax1.YLabel.Rotation = 90;
% ax1.YLabel.Position = [ -600 max(y1)/2 0];
line([min(x1) max(x1)], [min(y1) max(x1)*(60/1000)],'Linewidth',2)
grid on
% Lines in the plot
% Line 1 RED horizontal
Q = 200; %m^3/h
p1 = [min(x1) max(x1)];
p2 = [Q Q];
line(p1, p2,'color','red');
% Line 2 GREEN vertical
p3 = [Q Q]*(1000/60);
p4 = [min(x1) max(x1)];
line(p3, p4,'color','green');
% Secondary Axes
ax2 = axes(t);
ax2.XLim = ([min(x2) max(x2)]);
ax2.XTick=(x2);
ax2.XTickLabelRotation = 90;
ax2.XLabel.String = 'L/s';
ax2.YLim = ([min(y2) max(y2)]);
ax2.YTick = (y2);
ax2.YLabel.String = 'USGPM';
ax2.YLabel.Rotation = 90;
% ax2.YLabel.Position = [119 max(y2)/2 0];
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
% Change the aspect of the figure to x axes 50% of y axes to look similar
% to required plot
pbaspect(ax1,[0.5 1 1]);
pbaspect(ax2,[0.5 1 1]);
% From here on I'm trying ot get the figure into pdf format
% hold on
% clear ax1;
% clear ax2;
% linkaxes([ax1 ax2],'xy')
% zoom on
% ax = gcf;
%
% set(h,'PaperSize',[60 120]); %set the paper size to what you want
% print(h,'Output','-dpdf','-bestfit') % then print it
%
% % exportgraphics(ax,'Output.pdf','Resolution',300)
%
% get(ax,'PaperUnits')
% Link to following code
% http://www.ece.northwestern.edu/local-apps/matlabhelp/techdoc/printing/printse7.html
papersize = get(gcf,'PaperSize');
width = 12; % Initialize a variable for width.
height = 2*width; % Initialize a varible for height.
left = (papersize(1)- width)/2;
bottom = (papersize(2)- height)/2;
set(gcf, 'PaperPositionMode', 'manual');
myfiguresize = [left, bottom, width, height];
set(gcf, 'PaperPosition', myfiguresize);
% print

Respuestas (1)

Nivedita
Nivedita el 6 de Sept. de 2023
Editada: Nivedita el 15 de Sept. de 2023
Hi Gerard,
I understand that you are facing several issues with the plot. I will address them one by one:
1. There is no available property to align the tick labels. As a workaround, you can right-align the "yTickLabels" by padding whitespace to the left of the shorter labels. Here is how you can do this:
maxTickLength = max(strlength(ax2.YTickLabel));
ax2.YTickLabel = pad(ax2.YTickLabel,maxTickLength,'left');
However you should note that the alignment isn't perfect because the font is not fixed-width. If you are concerned with this, you can assign a fixed width font in the following manner:
ax2.FontName = 'fixedwidth';
ax1.FontName = 'fixedwidth';
Here is how it will look after assigning a fixed width font to both the axes:
2. I found another MATLAB answer that addresses the issue that you are facing while zooming in on the figure. You can access the workaround from this link:
3. I executed the code but could not reproduce the third issue you have mentioned. Check the output of the “papersize” variable and accordingly adjust the “width” and “height” variables. For example, “papersize” gave the output [8.5, 11] and thus I set the “width” and “height” as 8 and 11 respectively.
I hope this helps!
Regards,
Nivedita.

Categorías

Más información sobre Visual Exploration en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by