[英]Retrieving X and Y values from Matlab Plot
我有一個情節圖,當我們從情節圖中使用鼠標選擇特定數據點時,我想要檢索x和y坐標。
有任何想法嗎?
另一種選擇是使用按鈕向下功能:
function mouseExample()
h = plot(rand(10,1), 'o-');
set(h, 'ButtonDownFcn',@buttonDownCallback)
function buttonDownCallback(o,e)
p = get(gca,'CurrentPoint');
p = p(1,1:2);
title( sprintf('(%g,%g)',p) )
end
end
請注意,這不僅適用於“數據點”,而是適用於單擊線條的插值(x,y)位置。 您可以通過搜索最近的實際點來處理結果,並測試點擊是否在合理的半徑內以接受它。
顯然,使用數據光標模式比其他人注意到的要容易得多......
即使您沒有數據按鈕,也可以通過命令datacursormode
激活數據光標模式。 如果要存儲數據點而不是顯示它們,可以使用修改后的更新函數(基於matlab文檔中給出的示例):
function getDataFromFigure()
% Plots graph and sets up a custom data tip update function
fig = figure;
a = -16; t = 0:60;
plot(t,sin(a*t))
% variable to store data points
myData = [];
% enable data cursor mode
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',@myUpdateFcn)
set(dcm_obj, 'enable', 'on')
% do disable data cursor mode use
% set(dcm_obj, 'enable', 'off')
function txt = myUpdateFcn(dummy, event_obj)
% Customizes text of data tips
% read out data point
pos = get(event_obj,'Position');
% store data point
myData(end+1,:) = pos;
% no data shown on figure
txt = {''};
% or
% data also shown on figure:
% txt = {['Time: ',num2str(pos(1))],...
% ['Amplitude: ',num2str(pos(2))]};
end
end
〜編輯〜
確保函數myUpdateFcn
嵌套在main函數中(注意我示例底部的end
),以確保函數中已知myData。 如果無法嵌套,請將myData設為全局變量。
你的圖上方有一個叫做數據光標的小按鈕。 點擊它,然后雙擊你的圖,你應該得到你想要的。
您可以使用Plotly MATLAB API在懸停上獲取基於Web的圖形的文本。
將鼠標滑過某個點或單擊並拖動以進行縮放時,數據會顯示。 這是下圖的在線版本。 下圖顯示了懸停文字; 您還可以單擊“數據和圖形”鏈接以在網格中訪問圖形背后的數據。
注意:我在Plotly團隊。
api_path = '/path/to/plotly';
addpath(api_path);
api_key = 'key';
username = 'username';
signin(username, api_key);
x=ones(3000,1); x(1:1000) = 1; x(1001:2000) = 2; x(2001:3000) = 3;
y=ones(3000,1); y(1:1000) = lognrnd(0,1,1000,1); y(1001:2000) = lognrnd(0,2,1000,1); y(2001:3000) = lognrnd(0,3,1000,1);
s=struct('type','box','jitter',0.5);
layout = struct('title', 'Fun with the Lognormal distribution','yaxis',struct('type','log'));
plotly(x,y, struct('style', s));
response = plotlylayout(layout);
url = response.url
filename = response.filename
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.