gpt4 book ai didi

matlab - 如何在同一个图形上绘制三个图形?

转载 作者:行者123 更新时间:2023-12-04 05:36:07 25 4
gpt4 key购买 nike

这就是我绘制两个图形的方式(感谢帮助我做到这一点的人):

clear
logsFolder = 'C:\logs\';
stocks = {'log'};

for stock = stocks
filename = [logsFolder stock{1} '.log'];
fLog = fopen(filename);
data = textscan(fLog, '%f:%f:%f:%f %f %f %f');
fclose(fLog);

% hh:min:sec:millisec
secvec = [60*60 60 1 1e-3];
x = [data{1:4}] * secvec';

y = data{5};
yPrice = data{6};

xindays = x / (24*60*60);

figure;
[AX,H1,H2] = plotyy(xindays,y,xindays,yPrice);
set(AX(1),'xtick',[]);

lo1 = min(y);
hi1 = max(y);
lo2 = min(yPrice);
hi2 = max(yPrice);

if (hi2/lo2 > hi1/lo1)
ylim(AX(1),[lo1 hi2/lo2 * lo1]);
ylim(AX(2),[lo2 hi2]);
else
ylim(AX(1),[lo1 hi1]);
ylim(AX(2),[lo2 hi1/lo1 * lo2]);
end

ticklabelformat(AX(2),'y','%g')
ticklabelformat(AX(2),'x',{@tick2datestr,'x','HH:MM:SS'})
title(stock);

% iNeedToDrawThat = data{7}
end

输入文件示例可用 here如您所见,我的文件包含我也想显示的最后一列。范围应该是从 0(在图的底部)到最大值(在图的顶部)。所以我需要以某种方式绘制三个图形。可以省略第三张图的带标签的轴,因为我已经有两个轴,而我没有地方添加第三个。但是,如果可能,可以“重叠”两个轴。

我不知道该怎么做,所以我正在寻求您的帮助。

我试过了,但它不起作用:
figure;
[AX,H1,H2] = plotyy(xindays,y,xindays,yPrice);

hold on;
volume = data{7};
plot(xindays, volume);
hold off;

最佳答案

我已经提到了 similar question在评论中,它应该给你很多想法......

无论如何,我已经整理了一个解决方案来绘制多个 y 轴。现在代码有点复杂,但应该可以重构出一个可重用的函数(如文件交换中的 addaxis 函数)。

这个想法是首先在一个单独的轴上绘制每条曲线(全部叠加),并使它们透明(底部除外)。接下来,我们创建这组轴的副本并沿 x 方向移动它们。我们还使这些副本透明,但现在我们可以显示每个副本的 y 轴上的刻度线。最后,我们为它们提供正确的 z 顺序,并链接 x 和 y 限制,以便我们可以使用平移和缩放功能。

%# read and parse data from file
fid = fopen('log.log','rt');
C = textscan(fid, '%s %f %f %f', 'CollectOutput',true);
fclose(fid);
dt = datenum(C{1}, 'HH:MM:SS:FFF');
data = C{2};
NUM = size(data,2);

%# create a wider figure
hFig = figure('Position',get(0,'DefaultFigurePosition').*[1 1 1.7 1]);

%# some properties
clr = lines(NUM);
bgClr = get(0,'DefaultFigureColor');
pos = get(0,'DefaultAxesPosition');
pp = 0.1; % shift in normalized units: pos(1)

%# create plot axes (make axis invisible)
hAx = zeros(NUM,1);
for i=1:NUM
hAx(i) = axes('Parent',hFig, 'Color','none', ...
'XColor',bgClr, 'YColor',bgClr, ...
'Units','normalized', 'Position',pos+[(NUM-1)*pp 0 -(NUM-1)*pp 0]);
line(dt, data(:,i), 'Color',clr(i,:), 'Parent',hAx(i))
end
axis(hAx, 'tight') %# tight x/y limits

%# create shifted copies of axes to show y-ticks
hAxx = zeros(size(hAx));
for i=1:NUM
hAxx(i) = copyobj(hAx(i), hFig);
delete(get(hAxx(i),'Children')); %# keep only axis
set(hAxx(i), 'YColor',clr(i,:), ...
'Units','normalized', 'Position',pos+[(NUM-i)*pp 0 -(NUM-i)*pp 0]);
ylabel(hAxx(i), sprintf('Axis %d',i))
end
xlabel(hAxx(1), 'datetime')
title(hAxx(1), 'Log')
datetick(hAxx(1), 'x', 'HH:MM', 'keeplimits')

%# set 1st axis copy as current axis
set(hFig, 'CurrentAxes',hAxx(1))

%# adjust ticks of axes
set(hAx(1), 'Color','w') %# give white bg to 1st axis
set(hAxx(1), 'XColor','k') %# show xticks of 1st axis copy
set(hAxx(2:end), 'XTick',[], 'XTickLabel',[])
set(hAx, 'XTick',[], 'XTickLabel',[], 'YTick',[], 'YTickLabel',[])

%# fix z-order
for i=3:-1:1, uistack(hAxx(i),'top'), end
uistack(hAx(1), 'bottom')

%# link x/y limits so that panning/zooming works
lh = cell(NUM+1,1);
for i=1:NUM
lh{i} = linkprop([hAxx(i);hAx(i)], 'YLim');
end
lh{end} = linkprop([hAxx;hAx], 'XLim');

result :

screenshot

平移/缩放有点有趣,您必须通过从侧面开始拖动(移动的彩色轴)来启动它们。这是因为第一个(对应于蓝线)是最上面的一个,因此可以捕获所有鼠标点击。

注意:我看到您正在使用自定义函数 ticklabelformat ,我没有结合上面的代码测试过。我会把那部分留给你..

HTH

关于matlab - 如何在同一个图形上绘制三个图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11906580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com