我需要创作一部电影。假设,我创建了一个轴,并在其上绘制了一些非常定制的东西:
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)更新轴,并保留所有轴属性的最快、最简单的方法是什么?
发布于 2012-04-25 22:30:08
一种快速的方法是简单地更新您绘制的数据的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(:) );发布于 2012-04-25 22:27:23
只需将轴句柄传回后续的绘图命令即可
即
plot(ax, ...)而不是
ax = plot(...)https://stackoverflow.com/questions/10317423
复制相似问题