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!