1

我想为烛台图编写一个脚本,我可以在 Octave 和 Matlab 中运行。到目前为止,我使用以下代码:

D=[ '15-Jul-2013'
'16-Jul-2013'
'17-Jul-2013'
'18-Jul-2013'
'19-Jul-2013'
'22-Jul-2013'
'23-Jul-2013'
'24-Jul-2013'];
O=[25.93 26.39 26.37 26.75 25.82 25.99 26.10 26.32];
H=[26.43 26.75 26.78 26.77 26.11 26.13 26.30 26.53];
L=[25.60 26.01 26.30 26.12 25.60 25.72 25.97 26.05];
C=[26.28 26.32 26.65 26.18 25.88 26.05 26.13 26.51];

datapoints=length(C);
hold on;
for i=1:datapoints
    plot([i i],[L(i) H(i)],'linewidth',2,'Color','k');
    if C(i)>O(i)
        plot([i i],[O(i) C(i)],'linewidth',5,'color','r');
    else
        plot([i i],[O(i) C(i)],'linewidth',5,'color','g');
    end
end

hold off;
grid on;

xlim([0 datapoints+1]);
y=get(gca,'ylim');
ymin=int16(y(1)-0.5);
ymax=int16(y(2)+0.5);
ylim([ymin ymax]);

XTick=zeros(1,length(datapoints));
j=1;
for i=1:1:datapoints
    XTick(j)=i;
    j=j+1;
end


set(gca,'XTick',XTick,'XTickLabel','')    
pos = get(gca,'Position');
set(gca,'Position',[pos(1), .15, pos(3) .75])

for i=1:length(XTick)
    hText = text(XTick(i), double(ymin), D(XTick(i),:));
    set(hText,'Rotation',45,'HorizontalAlignment','right','VerticalAlignment','top');
end 

生成的图在 Matlab 中看起来相当不错,但在 Octave 中看起来非常糟糕。如何使两个程序中的情节看起来都不错?

4

2 回答 2

2

这里的解决方案是使用填充函数而不是绘图。

以下代码在 octave 和 matlab 中导致类似的结果,与绘图界面无关:

D=[ '15-Jul-2013'
'16-Jul-2013'
'17-Jul-2013'
'18-Jul-2013'
'19-Jul-2013'
'22-Jul-2013'
'23-Jul-2013'
'24-Jul-2013'];
O=[25.93 26.39 26.37 26.75 25.82 25.99 26.10 26.32];
H=[26.43 26.75 26.78 26.77 26.11 26.13 26.30 26.53];
L=[25.60 26.01 26.30 26.12 25.60 25.72 25.97 26.05];
C=[26.28 26.32 26.65 26.18 25.88 26.05 26.13 26.51];

     colorDown = 'g'; 
     colorUp = 'r'; 
     colorLine = 'k';
     date = (1:length(O))';

% w = Width of body, change multiplier to draw body thicker or thinner
% the 'min' ensures no errors on weekends ('time gap Fri. Mon.' > wanted
% spacing)
w=.1*min([(date(2)-date(1)) (date(3)-date(2))]);
%%%%%%%%%%%Find up and down days%%%%%%%%%%%%%%%%%%%
d=C-O;
l=length(d);
hold on
%%%%%%%%draw line from Low to High%%%%%%%%%%%%%%%%%
for i=1:l
   line([date(i) date(i)],[L(i) H(i)],'Color',colorLine, 'linewidth',2)
end
%%%%%%%%%%draw white (or user defined) body (down day)%%%%%%%%%%%%%%%%%
n=find(d<0);
for i=1:length(n)
    x=[date(n(i))-w date(n(i))-w date(n(i))+w date(n(i))+w date(n(i))-w];
    y=[O(n(i)) C(n(i)) C(n(i)) O(n(i)) O(n(i))];
    fill(x,y,colorDown)
end
%%%%%%%%%%draw black (or user defined) body(up day)%%%%%%%%%%%%%%%%%%%
n=find(d>=0);
for i=1:length(n)
    x=[date(n(i))-w date(n(i))-w date(n(i))+w date(n(i))+w date(n(i))-w];
    y=[O(n(i)) C(n(i)) C(n(i)) O(n(i)) O(n(i))];
    fill(x,y,colorUp)
end

    datapoints=length(C);
    xlim([0 datapoints+1]);
    y=get(gca,'ylim');
    ymin=int16(y(1)-0.5);
    ymax=int16(y(2)+0.5);
    ylim([ymin ymax]);

    XTick=zeros(1,length(datapoints));
    j=1;
    for i=1:2:datapoints
        XTick(j)=i;
        j=j+1;
    end

        set(gca,'XTick',XTick,'XTickLabel','')    
    pos = get(gca,'Position');
    set(gca,'Position',[pos(1), .15, pos(3) .75])

    for i=1:length(XTick)
        hText = text(XTick(i), double(ymin), D(XTick(i),:));
        set(hText,'Rotation',45,'HorizontalAlignment','right','VerticalAlignment','top');
    end

    hold off
于 2013-11-14T11:30:41.080 回答
1

这是我使用 Octave 3.6.2 得到的(我使用屏幕截图来说明它在屏幕上的实际外观):

  • 图形工具包qt

在此处输入图像描述

  • 图形工具包gnuplot

在此处输入图像描述

  • 图形工具包fltk

在此处输入图像描述

在所有 3 种情况下,蜡烛的主体都是垂直的。

于 2013-10-23T08:17:08.647 回答