Matlab 带有附加z轴的hist3绘图

Matlab 带有附加z轴的hist3绘图,matlab,plot,histogram,mesh,Matlab,Plot,Histogram,Mesh,以下代码为两个二维分布创建二维堆叠直方图: %%first dataset x1 = 200 + 300.*rand(1000,1)'; %rand values between 0 and 200 y1 = 100 + 250.*rand(1000,1)'; %rand values between 100 and 500 %%secnd dataset x2 = 100 + 200.*rand(1000,1)'; %rand values between 0 and 200 y2 =

以下代码为两个二维分布创建二维堆叠直方图:

%%first dataset
x1 = 200 + 300.*rand(1000,1)';  %rand values between 0 and 200
y1 = 100 + 250.*rand(1000,1)';  %rand values between 100 and 500
%%secnd dataset
x2 = 100 + 200.*rand(1000,1)';  %rand values between 0 and 200
y2 = 200 + 400.*rand(1000,1)';  %rand values between 100 and 500


one = linspace(100,400,20);
two = linspace(100,500,20);
EDGES = {one, two}; %edges

[n1,c1] = hist3([x1' y1'],'Edges',EDGES);%first dataset
[n2,c2] = hist3([x2' y2'],'Edges',EDGES);%second dataset


figure('Color','w');  

% plot the first data set

bh=bar3(n1);

% Loop through each row and shift bars upwards

for ii=1:length(bh)
      zz = get(bh(ii),'Zdata');
      kk = 1;

      % Bars are defined by 6 faces(?), adding values from data2 will
      % shift the bars upwards accordingly, I'm sure this could be made
      % better!
      for jj = 0:6:(6*length(bh)-6)  
             zz(jj+1:jj+6,:)=zz(jj+1:jj+6,:)+n2(kk,ii);
             kk=kk+1;
      end

    %erase zero height bars
    %# get the ZData matrix of the current group
    Z = get(bh(ii), 'ZData');

    %# row-indices of Z matrix. Columns correspond to each rectangular bar
    rowsInd = reshape(1:size(Z,1), 6,[]);

    %# find bars with zero height
    barsIdx = all([Z(2:6:end,2:3) Z(3:6:end,2:3)]==0, 2);

    %# replace their values with NaN for those bars
    Z(rowsInd(:,barsIdx),:) = NaN;

    %# update the ZData
    set(bh(ii), 'ZData',Z)
end

% Set face colour to blue for data1
set(bh,'FaceColor',[0 0 1]);

% Apply hold so that data2 can be plotted

hold on;

% Plot data2
bh=bar3(n2);
%erase zero height bars
for ii=1:numel(bh)
    %# get the ZData matrix of the current group
    Z = get(bh(ii), 'ZData');

    %# row-indices of Z matrix. Columns correspond to each rectangular bar
    rowsInd = reshape(1:size(Z,1), 6,[]);

    %# find bars with zero height
    barsIdx = all([Z(2:6:end,2:3) Z(3:6:end,2:3)]==0, 2);

    %# replace their values with NaN for those bars
    Z(rowsInd(:,barsIdx),:) = NaN;

    %# update the ZData
    set(bh(ii), 'ZData',Z)
end

% Set face color to red
set(bh,'FaceColor',[1 0 0]);

%set ticks
set(gca,'XTick',1:6:numel(one),'XTickLabel',one(1:6:end))
set(gca,'YTick',1:6:numel(one),'YTickLabel',one(1:6:end))

view(20,40)

%labels
xlabel('x')
ylabel('y')
zlabel('z')

%set transparency
set(gcf,'renderer','opengl');
set(get(gca,'child'),'FaceAlpha',0.8);
set(get(gca,'child'),'EdgeAlpha',0.3);

第一个问题是透明度(但我认为这是我的matlab版本2014a的问题,所以我不为此烦恼)。这只会让一切变得模糊

我的问题是如何在同一张图片上添加网格图。创建网格的代码如下所示:

%create surface I want to plot
[X,Y] = meshgrid(one,two);

inds1=find(X(:).*Y(:)<.3e5);%condition
inds2=find(X(:).*Y(:)>.3e5);

I=Y./X.^2;%first surface

I(inds1)=NaN;%second surface


figure('Color','w');hold on
mesh(X,Y,I,'FaceColor',[0 0 1],'EdgeColor','none')

I(:,:)=NaN;
I(inds1)=Y(inds1)./X(inds1);%second surface

mesh(X,Y,I,'FaceColor',[1 0 0],'EdgeColor','none')

alpha(.5)
grid on

view(20,40)

%labels
xlabel('x')
ylabel('y')
zlabel('z')
但这是不可行的,因为属性
ZAxisLocation
不存在

有人知道如何添加z轴吗

此外,如果您对如何改进代码有其他意见,欢迎发表

感谢

二维叠加直方图:


擦除历史图中的零值:

我现在认为这还不可能()

所以我添加了一个假轴:

[X,Y] = meshgrid(one,two);

inds1=find(X(:).*Y(:)<.3e5);%condition
inds2=find(X(:).*Y(:)>.3e5);

s=Y./X.^2;%first surface

s(inds1)=NaN;%second surface

%mesh(X,Y,I,'FaceColor',[0 0 1],'EdgeColor','none')
mesh((X-min(min(X)))/max(max(X-min(min(X))))*20,(Y-min(min(Y)))/max(max(Y-min(min(Y))))*20,...
    s/max(max(s))*max(max(n1))+max(max(n1)),'FaceColor','g','EdgeColor','none','facealpha',.5)

s(:,:)=NaN;
s(inds1)=Y(inds1)./X(inds1);%second surface

%mesh(X,Y,I,'FaceColor',[1 0 0],'EdgeColor','none')
mesh((X-min(min(X)))/max(max(X-min(min(X))))*20,(Y-min(min(Y)))/max(max(Y-min(min(Y))))*20,...
    s/max(max(s))*max(max(n1))+max(max(n1)),'FaceColor','y','EdgeColor','none','facealpha',.5)

alpha(.5)
grid on

%add fake z axis
line([20 20],[1 1],[max(max(n1))-min(min(s)) 20],...
    'color','g','linewidth',2)

% text(20*ones(1,5),zeros(1,5),linspace(max(max(n1))-min(min(I)),20,5),...
%     num2str(linspace(0,max(max(I)),5)),'color','g')

z=linspace(max(max(n1))-min(min(s)),20,5);
txto=linspace(0,max(max(s)),5);

for ii=1:5
    line([20 20.3],[1 1],[z(ii) z(ii)],'color','g')%ticks
    text(20,0,z(ii),num2str(txto(ii)),'color','g')%ticklabel
end
text(19.8,1,21,'s','color','g')%label
[X,Y]=meshgrid(一,二);
inds1=find(X(:).*Y(:).3e5);
s=Y./X.^2;%第一表面
s(inds1)=NaN;%第二表面
%网格(X,Y,I,'FaceColor',[0 0 1],'EdgeColor','none')
网格((X-min(min(X)))/max(max(X-min(min(X))))*20,(Y-min(min(Y)))/max(max(Y-min(min(Y)))*20,。。。
s/max(max(s))*max(max(n1))+max(max(n1))、'FaceColor'、'g'、'EdgeColor'、'none'、'facealpha'、.5)
s(:,:)=NaN;
s(inds1)=Y(inds1)。/X(inds1);%第二表面
%网格(X,Y,I,'FaceColor',[10],'EdgeColor','none')
网格((X-min(min(X)))/max(max(X-min(min(X))))*20,(Y-min(min(Y)))/max(max(Y-min(min(Y)))*20,。。。
s/max(max(s))*max(max(n1))+max(max(n1))、'FaceColor'、'y'、'EdgeColor'、'none'、'facealpha'、.5)
α(.5)
网格化
%添加假z轴
行([20 20],[11],[max(max(n1))-min(min(s))20],。。。
‘颜色’、‘g’、‘线宽’,2)
%文本(20*1(1,5),零(1,5),linspace(max(max(n1))-min(min(I)),20,5),。。。
%num2str(linspace(0,max(max(I)),5)),'color','g')
z=linspace(max(max(n1))-min(min(s)),20,5);
txto=linspace(0,max(max(s)),5);
对于ii=1:5
行([20.3],[11],[z(ii)z(ii)],'color','g')%ticks
文本(20,0,z(ii),num2str(txto(ii)),“颜色”和“g')%
结束
文本(19.8,1,21,'s','color','g')%标签


总而言之,代码非常难看,需要进行大量调整。

我现在认为这还不可能()

所以我添加了一个假轴:

[X,Y] = meshgrid(one,two);

inds1=find(X(:).*Y(:)<.3e5);%condition
inds2=find(X(:).*Y(:)>.3e5);

s=Y./X.^2;%first surface

s(inds1)=NaN;%second surface

%mesh(X,Y,I,'FaceColor',[0 0 1],'EdgeColor','none')
mesh((X-min(min(X)))/max(max(X-min(min(X))))*20,(Y-min(min(Y)))/max(max(Y-min(min(Y))))*20,...
    s/max(max(s))*max(max(n1))+max(max(n1)),'FaceColor','g','EdgeColor','none','facealpha',.5)

s(:,:)=NaN;
s(inds1)=Y(inds1)./X(inds1);%second surface

%mesh(X,Y,I,'FaceColor',[1 0 0],'EdgeColor','none')
mesh((X-min(min(X)))/max(max(X-min(min(X))))*20,(Y-min(min(Y)))/max(max(Y-min(min(Y))))*20,...
    s/max(max(s))*max(max(n1))+max(max(n1)),'FaceColor','y','EdgeColor','none','facealpha',.5)

alpha(.5)
grid on

%add fake z axis
line([20 20],[1 1],[max(max(n1))-min(min(s)) 20],...
    'color','g','linewidth',2)

% text(20*ones(1,5),zeros(1,5),linspace(max(max(n1))-min(min(I)),20,5),...
%     num2str(linspace(0,max(max(I)),5)),'color','g')

z=linspace(max(max(n1))-min(min(s)),20,5);
txto=linspace(0,max(max(s)),5);

for ii=1:5
    line([20 20.3],[1 1],[z(ii) z(ii)],'color','g')%ticks
    text(20,0,z(ii),num2str(txto(ii)),'color','g')%ticklabel
end
text(19.8,1,21,'s','color','g')%label
[X,Y]=meshgrid(一,二);
inds1=find(X(:).*Y(:).3e5);
s=Y./X.^2;%第一表面
s(inds1)=NaN;%第二表面
%网格(X,Y,I,'FaceColor',[0 0 1],'EdgeColor','none')
网格((X-min(min(X)))/max(max(X-min(min(X))))*20,(Y-min(min(Y)))/max(max(Y-min(min(Y)))*20,。。。
s/max(max(s))*max(max(n1))+max(max(n1))、'FaceColor'、'g'、'EdgeColor'、'none'、'facealpha'、.5)
s(:,:)=NaN;
s(inds1)=Y(inds1)。/X(inds1);%第二表面
%网格(X,Y,I,'FaceColor',[10],'EdgeColor','none')
网格((X-min(min(X)))/max(max(X-min(min(X))))*20,(Y-min(min(Y)))/max(max(Y-min(min(Y)))*20,。。。
s/max(max(s))*max(max(n1))+max(max(n1))、'FaceColor'、'y'、'EdgeColor'、'none'、'facealpha'、.5)
α(.5)
网格化
%添加假z轴
行([20 20],[11],[max(max(n1))-min(min(s))20],。。。
‘颜色’、‘g’、‘线宽’,2)
%文本(20*1(1,5),零(1,5),linspace(max(max(n1))-min(min(I)),20,5),。。。
%num2str(linspace(0,max(max(I)),5)),'color','g')
z=linspace(max(max(n1))-min(min(s)),20,5);
txto=linspace(0,max(max(s)),5);
对于ii=1:5
行([20.3],[11],[z(ii)z(ii)],'color','g')%ticks
文本(20,0,z(ii),num2str(txto(ii)),“颜色”和“g')%
结束
文本(19.8,1,21,'s','color','g')%标签

总的来说,代码非常难看,需要进行大量调整