我知道Stack-Overflow上有很多类似的问题,但是我找不到我特别感兴趣的那个,所以我要再问一遍 .

我正在创建一个图表,其中线条图覆盖着堆积条形图 . 我不是在使用plotyy . 我通过创建第二个轴系统来遵循此处提供的说明:http://www.mathworks.com/help/matlab/ref/plotyy.html

基本上,我绘制我的第一个数据集,获取当前轴的位置,在同一位置创建第二个轴,将y轴向右移动,x轴向上移动,并绘制基于的第二个/第三个数据集新轴 .

我的数据集基本上是:

x
y1 (axis 1),y2,y3 (axis 2)

虽然我能够使用所有线条样式在两个轴上绘制y1,y2,y3,但是我无法使用y2和y3作为条形样式 . 第二个轴以某种方式粘在第一个轴上,而不是向右上方移动 . 第一个数据集行也消失了 .

correct line plot

messed up line+bar plot

我还有一个小问题是如何去除第二轴的x轴(因为它们基本相同) . 我在网上搜索,他们说将xtick设置为[] . 但我收到错误:使用命令无效或删除对象

set(ax1,'YTick',[])

非常感谢你 .

正如我所指出的,我没有上传代码,在这里你去;)

% this script predicts 

% user prompt
prompt = {'Stock Name:','Cost per share($):','Current Value ($):','Holdings (shares):','Est. High ($)','Tolerance ($):'};
user_input = inputdlg(prompt);

% process user input
if isempty(user_input)
    stockname = 'APPLE.INC';
    x0 = 125.82;
    xn = 129.91;
    N0 = 80;
    xt = 135;
    tol = 20;
else
    [stockname,x0,xn,N0,xt,tol] = user_input{:};
    x0 = str2num(x0);
    xn = str2num(xn);
    xt = str2num(xt);
    N0 = str2num(N0);
    tol = str2num(tol);
end

% calculate sale-rebuy threshold 
xt = linspace(x0-tol,xt+tol,10);
[x0,xn,y,N0,Ny] = sale_rebuy(x0,xn,xt,N0);
profit_rebuy = Ny.*(xt-y);
profit_nosale = N0*(xt-x0);

% plotting
figure
line(xt,y,'Color','r','LineStyle','--');

ax1=gca;
set(ax1,'XColor','r');
set(ax1,'YColor','r');
ax1_pos = get(ax1,'Position');
ax2 = axes('Position',ax1_pos,...
    'XAxisLocation','top',...
    'YAxisLocation','right',...
    'Color','none');

profit = [profit_rebuy;profit_nosale]';
%bar(ax2,xt,profit,'stacked');
line(xt,profit_rebuy,'Parent',ax2,'Color','k','LineStyle',':');

title(stockname);
xlabel(ax1,'final price');
xlabel(ax2,'final price');
ylabel(ax1,'rebuy price');
ylabel(ax2,'profit');


% This is the function
function [x0,xn,y,N0,Ny] = sale_rebuy(x0,xn,xt,N0)
  y = (xn.*xt)./(xt-x0+xn);
  Ny = xn.*N0./y;

  x0 = x0;
  xn = xn;
  N0 = N0;
end