How to change the desired xaxis ticks and subsequent values in log log plot?

조회 수: 16 (최근 30일)
I have a flood frequency analysis data stored in a file named "Matlabdataforgraph.txt." My intention is to visualize this data using MATLAB. To achieve this, I have specified a set of desired tick marks for the x-axis, arranged in reverse order. Successfully incorporating these ticks into my MATLAB plot, I now seek to create a logarithmic scale for the y-axis. However, I am encountering an issue where the x-axis values from the text file are not aligning with the desired tick marks in the plot, resulting in a deviation from the anticipated straight line in the plot. I have provided the data, MATLAB code, and an image illustrating the expected curve for your reference. Thanks in advance
MATLAB CODE
Q = importdata('Matlabdataforgraph.txt');
T1= rmmissing(Q(:,3));
T2= rmmissing(Q(:,4));
T3= rmmissing(Q(:,5));
T4= rmmissing(Q(:,8));
figure;
semilogy(T1,T4,'or','linewidth', 1.5)
hold on
semilogy(T1,T2,'--k','linewidth', 1.5)
semilogy(T1,T3,'--b','linewidth', 1.5)
semilogy(Q(:,1), Q(:,2),'o','MarkerEdge',[0 0 0],'MarkerSize',8,'MarkerFaceColor',[.7 .7 .7]);
desiredTicks = [1 5 20 40 60 80 90 95 98 99.5]; % Set the desired tick positions
numTicks = numel(desiredTicks);
logTicks = logspace(log10(desiredTicks(1)), log10(desiredTicks(end)), numTicks);
set(gca, 'XTick', logTicks);
set(gca, 'XTickLabel', arrayfun(@num2str, desiredTicks, 'UniformOutput', false));
xlim([1, 100])
mappedDataX = interp1(logspace(log10(desiredTicks(1)), log10(desiredTicks(end)), numel(desiredTicks)), desiredTicks, T1);
% Interpolate values in T1 to find their equivalent positions in the original scale
interpValues_original_scale = interp1(desiredTicks, logTicks, T1, 'linear', 'extrap');
hold on
a1 = Q(:, 3);
a1 = a1(~isnan(a1));
b1 = Q(:, 4);
b1 = b1(~isnan(b1));
c1 = Q(:, 5);
c1 = c1(~isnan(c1));
legend({'50% confidence level','95% confidence level',' 5% confidence level','Gaged peak Q (Bulletin 17B)','PILF (LO)'},'Location','Northwest','fontsize',12)
legend boxoff
ylim([1,1000000]);
ax = gca;
ax.XScale = 'log';
ax.XDir = 'reverse';
xlabel('Annual Exceedance Probability (%)','fontweight','bold','fontsize',18);
ylabel('Annual Maximum Discharge (cumecs)','fontweight','bold','fontsize',18);
set(gca,'linewidth', 1.5,'fontsize',12,'fontname','Times New Roman')
grid on
gridColor = [0.7, 0.7, 0.7]; % Light gray color
set(gca, 'GridColor', gridColor);
set(gca, 'MinorGridColor', gridColor);
set(gca, 'GridLineStyle', '-');
set(gca, 'MinorGridLineStyle', '-');
gridLineWidth = 0.7;
set(gca, 'LineWidth', gridLineWidth);
saveas(gcf, 'sample_figure.png');

답변 (1개)

Constantino Carlos Reyes-Aldasoro
Hello
There are 2 separate properties that you have to handle, one is the ticks and the other is the ticklabels, and both are better managed if you use the handles effectively, for example
y=[ 0 1 2 9 6 5 9]; x = [1 2 3 4 5 6 7];
hAxis = gca;
hPlot = plot(x,y,'o');
If you do this in Matlab local, it will display a figure, here it will be displayed below.
Notice that I saved the handles of the axis and of the plot. Now we can modify the ticks and the labels.
hAxis.XTick = [1 1.5 2 3 5 6 11];
hAxis.YTickLabel{3}='three';
hAxis.YTickLabel{5}='F I V E';
In the lines above, I changed the values of xtick so that the ticks are placed in a certain location, not necessarily the same as the values of the data, and changed the values of yticklabel to anything you want.
I think if you use the properties, you will be able to fix your problem accordingly.

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by