2016-11-30 4 views
3

は等高線図を作成するには、次の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はコマンドウィンドウで変数を表示、または強制科学表記方法に匹敵する)

enter image description here

答えて

6

あなたが文書化されていないMarkedCleanイベントを使用してこれを行うことができます。

残念ながら、Matlabはプロットが再描画されるたびにテキストを更新します(figure resizeなど)。そのため、リスナーを追加してその都度更新する必要があります。

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 
+1

私はこの答えは非常に適しだろうと感じ[**文書化されていないが、実効状態DOC **](http://stackoverflow.com/documentation/matlab/2383/undocumented-features#t=201612020612571995264)、もう少し説明して、すてきなスクリーンショットが必要です。 – thewaywewalk

+0

私は[example](http://stackoverflow.com/documentation/matlab/2383/undocumented-features/26052/contour-plots-customise-the-text-labels)を追加しました。 – matlabgui

関連する問題