How to prevent contour plot from setting axis limits?

35 visualizaciones (últimos 30 días)
Ethan Narad
Ethan Narad el 13 de En. de 2022
Comentada: Dave B el 13 de En. de 2022
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?

Respuesta aceptada

Dave B
Dave B el 13 de En. de 2022
Editada: Dave B el 13 de En. de 2022
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 comentarios
Ethan Narad
Ethan Narad el 13 de En. de 2022
Hi Dave!
I should've clarified, adding more data will cause the axes to stretch to fit the data when there's a contour on the plot, as shown in your first plot.
I tried hold on, manually resetting the axis limits, and using the size restriction on the axes. I would've included code but it's split across several functions.
However, I was able to exploit the process of adding more data by saving the original axis size and adding a single white datapoint proportional to the original aspect ratio, so when the contour (or whatever is causing) re-sizes the plot, it crops it to my new fake datapoint and effectively (though jankily) solves the problem.
Dave B
Dave B el 13 de En. de 2022
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...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by