是否可以用科学计数法标记等高线图?
Is it possible to label contour plot in scientific notation?
考虑以下 MWE 来创建等高线图:
close all
[X,Y]=meshgrid(0:100,0:100);
Z=(X+Y.^2)*1e10;
[C,h]=contour(X,Y,Z);
h.ShowText='on';
但是,标签始终显示轮廓的完整整数符号。 是否有合理的方法来改变这种行为?(例如,类似于 MATLAB 在命令 window 中显示变量的方式或强制科学记数法)
您可以使用未记录的 MarkedClean
事件来执行此操作。
不幸的是,每次重绘绘图(例如调整图形大小)时 Matlab 都会更新文本 - 因此您需要添加一个侦听器以在每次发生时更新它们 - 因此您为什么要侦听此特定事件。
function test
figure
[X,Y]=meshgrid(0:100,0:100);
Z=(X+Y.^2)*1e10;
[C,h]=contour(X,Y,Z);
h.ShowText='on';
% add a listener and call your new format function
addlistener(h,'MarkedClean',@(a,b)ReFormatText(a))
end
function ReFormatText(h)
% get all the text items from the contour
t = get(h,'TextPrims');
for ii=1:length(t)
% get the current value (Matlab changes this back when it
% redraws the plot)
v = str2double(get(t(ii),'String'));
% Update with the format you want - scientific for example
set(t(ii),'String',sprintf('%0.3e',v));
end
end
考虑以下 MWE 来创建等高线图:
close all
[X,Y]=meshgrid(0:100,0:100);
Z=(X+Y.^2)*1e10;
[C,h]=contour(X,Y,Z);
h.ShowText='on';
但是,标签始终显示轮廓的完整整数符号。 是否有合理的方法来改变这种行为?(例如,类似于 MATLAB 在命令 window 中显示变量的方式或强制科学记数法)
您可以使用未记录的 MarkedClean
事件来执行此操作。
不幸的是,每次重绘绘图(例如调整图形大小)时 Matlab 都会更新文本 - 因此您需要添加一个侦听器以在每次发生时更新它们 - 因此您为什么要侦听此特定事件。
function test
figure
[X,Y]=meshgrid(0:100,0:100);
Z=(X+Y.^2)*1e10;
[C,h]=contour(X,Y,Z);
h.ShowText='on';
% add a listener and call your new format function
addlistener(h,'MarkedClean',@(a,b)ReFormatText(a))
end
function ReFormatText(h)
% get all the text items from the contour
t = get(h,'TextPrims');
for ii=1:length(t)
% get the current value (Matlab changes this back when it
% redraws the plot)
v = str2double(get(t(ii),'String'));
% Update with the format you want - scientific for example
set(t(ii),'String',sprintf('%0.3e',v));
end
end