How to prevent contour plot from setting axis limits?
27 次查看(过去 30 天)
显示 更早的评论
I'm trying to add a contour plot over a regular plot on the same axes. It almost works, but every time I add the contour plot to the figure, it overrides the axis size. This is done within the app editor, but I had the same problem in regular Matlab. As far as I can tell, the problem is that there is something inherent to the Contour command that crops the axes to the available data, which in my case makes it harder to see what's on the plot.
Is there some way to force a contour plot to exist on a larger canvas than its data fills?
0 个评论
采纳的回答
Dave B
2022-1-13
编辑:Dave B
2022-1-13
contour doesn't reset the limits, perhaps you didn't set hold to on? or didn't specify x and y for your contour? If you're still having trouble, it might be helpful to include some code...
[x,y,z]=peaks;
plot([-10 10],[-10 10])
hold on
contour(x,y,z)
If, on the other hand, you want the contour to be bigger than the axes, set your limits (or just the LimitMode property) before adding the contour:
clf
[x,y,z]=peaks;
plot([-1 1],[-1 1])
set(gca,'XLimMode','manual','YLimMode','manual')
% alternatively: xlim manual;ylim manual
% alternatively: set(gca,'XLim',get(gca,'XLim')); set(gca,'YLim',get(gca,'YLim'));
hold on
contour(x,y,z)
2 个评论
Dave B
2022-1-13
I'm glad it's all worked out. I'm just dropping a note to clarify the rules, because I know it can be a little confusing and also because I don't want you to have to rely on something that you feel is janky!
- When you set the Lim properties, MATLAB toggles the corresponding LimMode properties from 'auto' to 'manual'
- If the *LimMode properties are 'auto', MATLAB will pick limits when a new object is added to the axes (in general, some objects - like Text - don't participate in limit selection)
- If hold isn't on (really if the axes NextPlot property is set to replace) then, when you call a plotting command like contour, the axes is reset. This means that not only are the contents of the axes removed, but the axes properties (including the LimMode ones) are reset.
- uiaxes makes this a tad more complicated, because the NextPlot property defaults to replacechildren, which means the contents are removed but the properties are not reset. That normally makes sense for an app.
- The x and y limit selection is not entirely independent, for instance, if your x limits are manual and your y limits are auto, the y limits will be determined by the range of data that your x limits show. (e.g. plot(1:10); xlim([5 6]); will set y limits to [5 6]);
- Starting in 2021a you can control the method that for selecting limits for each axis. This used to be controlled at the level of the whole axes with the axis command (i.e. axis tight, axis padded). You can control these with the [X/Y/Z]LimitMethod properties.
- One last wrinkle: if you set the lower limit to -inf or the upper limit to inf, MATLAB will choose a limit that reflects the extents of the objects in your axes, even though the LimMode will be 'manual').
Whew - limit selection is complicated, it's one of those things that most of the time you can just take for granted, because (I hope) we do a pretty good job with the majority of cases via our default automatic behavior. But sometimes when you want something a little more particular the can of worms starts to open up! Don't even get me started on how we pick where the ticks go...
更多回答(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!