Squeeze some part of a plot

15 Ansichten (letzte 30 Tage)
Lida
Lida am 2 Mai 2024
Beantwortet: Image Analyst am 3 Mai 2024
Hi, I'd like to squeze the above part of my plot so that the part after the break is 40% of the total y-axis and the below part is 60%. Is it possible to do that? Thank you very much.
  4 Kommentare
MarKf
MarKf am 3 Mai 2024
Oh I see I got the question wrong, sorry (I wasn't sure so I commented instead of answering). Thanks @Mathieu NOE.
There are so many about this too: FEX: break axis.
Lida
Lida am 3 Mai 2024
I do not want to put the break. I want to squeeze the part above the break and strech the part below the break.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Abhishek Kumar Singh
Abhishek Kumar Singh am 3 Mai 2024
Hi Lida,
I don't think MATLAB provides built-in functionality for such specific non-linear axis adjustments. But one can approximate this by manipulating the plot space visually. I got a resource from stackoverflow which might be helpful in your case:
Let's try this approach for a sine wave with amplitude 10. Assume the requirement of allocating 60% of the plot area to the range from 1 to 2, and 40% to the range from 2 to 10.
  1. We need a piecewise function that maps the y-values from 1 to 2 to a larger range than the y-values from 2 to 10. Let's define a simple linear transformation for this purpose:
% Transformation function
% This is a conceptual function; the actual implementation may require adjustment
function y_transformed = transformY(y)
if y >= 1 && y <= 2
% Map 1-2 to a larger range, e.g., 0-6 for 60% of the plot area
y_transformed = (y - 1) * 3; % Adjust the scaling factor as needed
elseif y > 2 && y <= 10
% Map 2-10 to a smaller range, e.g., 6-10 for 40% of the plot area
y_transformed = (y - 2) * 0.5 + 6; % Adjust the scaling factor as needed
else
y_transformed = y; % For values outside 1-10, no transformation
end
end
2. Let's create a sine wave with an amplitude of 10 and apply the transformation to the y-values.
x = linspace(0, 2*pi, 1000); % Generate x values
y = 10 * sin(x); % Original sine wave
% Apply the transformation to y-values
y_transformed = arrayfun(@(y) transformY(y), y);
3. Now, plot the transformed data and adjust the y-axis ticks and labels to reflect the original scale.
plot(x, y_transformed);
ylim([0 10]); % Adjust based on the transformed scale
% Set custom y-ticks and labels to reflect the original y-values
yticks = 1:10; % Original y-tick values
yticklabels = arrayfun(@(y) sprintf('%d', y), yticks, 'UniformOutput', false);
set(gca, 'YTick', arrayfun(@(y) transformY(y), yticks), 'YTickLabel', yticklabels);
The transformation function transformY is conceptual and needs to be adjusted based on the exact visual requirement. The scaling factors were chosen to illustrate the approach and may not perfectly match the 60%/40% area distribution. Depending on your exact requirements, a more complex function (e.g., logarithmic for one range and linear for another) might be necessary.
  1 Kommentar
Lida
Lida am 3 Mai 2024
Thank you so much for your constructive reply. I appreciate that.

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 3 Mai 2024
There are several submissions in the File Exchange for broken y axes or axes with "gaps" in them. Search for "break y axis"

Community Treasure Hunt

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

Start Hunting!

Translated by