getframe captures only a corner of the axes
13 次查看(过去 30 天)
显示 更早的评论
I am running the following code to try to capture an axis, as shown in the leftmost attached image.
figure;
plot(1:10, 1:10);
frame = getframe;
figure;
imagesc(frame.cdata)
The output of the above code is in the second attached image. As you can see, it results in an inexplicably cropped version of the axes, where only the bottom left corner of the axes is captured. I tried playing with the figure and axis position, as well as different settings of the axis ("tight," "off," etc.) but to no avail. The same thing happens with getframe(gca), so specifying the axis does not help. I also tried playing with getframe(gca, Rect) -- for example, I do getframe(gca, getpixelposition(gca)), but I got an error that the rectangle doesn't fit within the figure.
If I use getframe(gcf), that doesn't happen, but capturing the whole figure adds some extra whitespace even when I do "axis off", as can be seen in the last attached image, where I have plotted the resulting frame.cdata with imagesc. I don't want that deadspace. I want to capture the axis exactly as it is, no more, no less. In addition, I don't want to use exportgraphics or anything else. I want the underlying pixel values as an array.
Finally, I tried using the print function, but that didn't work either. It has the same sort of issues as explained in the previous paragraph with adding whitespace to the captured figure.



1 个评论
Walter Roberson
2025-8-6
I wonder if it is due to a known bug, described at https://www.mathworks.com/matlabcentral/answers/2177698-strange-behaviour-of-plot#answer_1566533
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Printing and Saving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!