我需要制作一部电影。假设,我创建了一个轴并在其上绘制了一些非常定制的东西:
figure;
ax = plot(x, y, 'linewidth', 3, 'prop1', value1, 'prop2', value2, ...);
grid minor;
axis(ax, [xmin xmax ymin ymax]);
legend(ax, ...);
xlabel(ax, ...);
ylabel(ax, ...);
title(ax, ...);
现在我运行一个循环,其中只有 y
的值被更新。
for k = 1 : N
% y changes, update the axis
end
用新的 y
(或 x
和 y
)更新轴的最快和最简单的方法是什么,同时保留所有轴属性?
最佳答案
一种快速的方法是简单地更新您绘制的数据的 y 值:
%# note: plot returns the handle to the line, not the axes
%# ax = gca returns the handle to the axes
lineHandle = plot(x, y, 'linewidth', 3, 'prop1', value1, 'prop2', value2, ...);
%# in the loop
set(lineHandle,'ydata',newYdata)
编辑 如果有多行怎么办,即 lineHandle
是一个向量?您仍然可以一步更新;不过,您需要将数据转换为元胞数组。
%# make a plot with random data
lineHandle = plot(rand(12));
%# create new data
newYdata = randn(12);
newYcell = mat2cell(newYdata,12,ones(1,12));
%# set new y-data. Make sure that there is a row in
%# newYcell for each element in lineH (i.e. that it is a n-by-1 vector
set(lineHandle,{'ydata'},newYcell(:) );
关于matlab - 在 Matlab 轴中,如何在保持所有轴属性的同时只更新数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10317423/