Squeeze some part of a plot
15 次查看(过去 30 天)
显示 更早的评论
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 个评论
MarKf
2024-5-3
Oh I see I got the question wrong, sorry (I wasn't sure so I commented instead of answering). Thanks @Mathieu NOE.
回答(2 个)
Abhishek Kumar Singh
2024-5-3
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.
- 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.
Image Analyst
2024-5-3
There are several submissions in the File Exchange for broken y axes or axes with "gaps" in them. Search for "break y axis"
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!