- Fill: https://www.mathworks.com/help/matlab/ref/fill.html?searchHighlight=fill&s_tid=srchtitle_support_results_1_fill
- Uistack: https://www.mathworks.com/help/matlab/ref/uistack.html?searchHighlight=uistack&s_tid=srchtitle_support_results_1_uistack
How to place a colored rectangle in background of part of a figure which has a logarithmic axis
조회 수: 10 (최근 30일)
이전 댓글 표시
I am trying to place a gray rectangle in the background of part of a figure. The figure has a logarithmic y-axis. I can place the rectangle just fine as long as I do not try to scale the axis. When I do that, I get the error message
Warning: Error updating Rectangle.
DataSpace or ColorSpace transform method failed.
Any suggestions? I have tried including the rectangle code before and after the rest of the commands for the figure.
댓글 수: 0
답변 (1개)
Jaswanth
2024년 8월 2일
Hi,
To place a coloured rectangle in the background of a figure with a logarithmic y-axis in MATLAB, you can use the fill function instead of the rectangle function.
Please refer to the following example code demonstrating the implementation discussed above:
% Sample data
x = linspace(1, 10, 100);
y = exp(x);
% Create figure
figure;
hold on;
% Plot data with logarithmic y-axis
plot(x, y);
set(gca, 'YScale', 'log');
% Define the coordinates for the rectangle
x_rect = [2, 8, 8, 2];
y_rect = [1e2, 1e2, 1e4, 1e4];
% Plot the rectangle using fill
fill(x_rect, y_rect, [0.8 0.8 0.8], 'EdgeColor', 'none');
% Bring the plot to the front
uistack(plot(x, y), 'top');
In the above example, x_rect and y_rect define the vertices of the rectangle.
The fill function is used to draw the rectangle, and the color ‘[0.8 0.8 0.8]’ corresponds to gray. The uistack function ensures that the plot remains on top of the rectangle.
Kindly refer to following MathWorks documentation to know more about the functions discussed above discussed above:
I hope the solution provided above is helpful.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!