【问题标题】:copyobj legend and facealpha issue when setting log yscale设置日志 yscale 时的 copyobj 图例和 facealpha 问题
【发布时间】:2016-05-20 11:53:37
【问题描述】:

我有以下绘制图片的代码:

%scamp data
[flipedBV, flipeddepth] = flipit(depthi, BVsurf_int_ens);

i=linspace(0,5,100);
edges_eps=10.^(-i); 
logedg_BV= log10(fliplr(edges_eps)); 

[n_BV,xout_BV] = histc(histazza(log10(flipedBV)), logedg_BV);

x = logedg_BV;

%model data
[n_BVn,xout_BVn] = histc(histazza(log10(sqrt(-BVsurf_Num_ens))), logedg_BV);

BVsurfF = figure;hold on
h=area(x,n_BV/sum(n_BV),'facecolor',[1 0 0]);  %%red area where the problem gonna be

legend('SCAMP')
xlabel('$$ N~[1/s]$$','Interpreter','latex','fontsize',18)
set(gca,'fontsize',14,'ygrid','on')
alpha(.5)  %%translucency of the red area

%%add new data
addLineToFig('KEPflu', [0 0 1], BVsurfF, 'BVsurf_Num_ens', logedg_BV);

addLineToFig('KEPflu2', [0 1 1], BVsurfF, 'BVsurf_Num_ens', logedg_BV);

addLineToFig('GASflu', [0 0 0], BVsurfF, 'BVsurf_Num_ens', logedg_BV);

addLineToFig('GASflu2', [1 0 1 ], BVsurfF, 'BVsurf_Num_ens', logedg_BV);

addLineToFig('EPSmin', [1 1 0], BVsurfF, 'BVsurf_Num_ens', logedg_BV);

addLineToFig('GASmin', [.5 .5 0], BVsurfF, 'BVsurf_Num_ens', logedg_BV);

addLineToFig('canuto', [.5 .5 .5]', BVsurfF, 'BVsurf_Num_ens', logedg_BV);

子程序addLineToFig包含在:

function addLineToFig(name, ccol, fighandle, variab, x)%, flippo, depthi)

cd(['E:\SIMULATIONS\',name,'\COMPARED\ensamble']);
load([name,'_ensamble'], variab);


[n_BVn, xout_BVn] = histc(histazza(log10(sqrt(-BVsurf_Num_ens))), x); %%new data

figure(fighandle)

[LEGHbv,OBJHbv,OUTHbv,OUTMbv] = legend; 
P=plot(x,n_BVn/sum(n_BVn),'color',ccol,'linewidth',2); %%plot new data
legend([OUTHbv;P],OUTMbv{:},name) %%update legend

end

基本上,我创建一个红色区域的图,然后用addLineToFig添加数据并正确获取:

当我尝试复制图形时出现问题:

h1=gcf;
h2=figure;  
objects=allchild(h1);
copyobj(get(h1,'children'),h2);
set(gca,'yscale','log')

如您所见,红色分布的半透明没有重复,图例有些问题。

问题似乎是我将 yscale 设置为记录的最后一行。如果我评论它,代码可以正常工作。有人知道解决方法吗?

最少的代码

i=linspace(0,5,100);
edges_eps=10.^(-i); 
logedg_BV= log10(fliplr(edges_eps)); 

a = 1e-5;
b = 1e-2;
r = (b-a).*rand(1000,1) + a;


[n_BV,xout_BV] = histc(histazza(log10(r)), logedg_BV);

x = logedg_BV;

%model data
r2 = (b-a).*rand(1000,1) + a;
[n_BVn,xout_BVn] = histc(histazza(log10(r2)), logedg_BV);

BVsurfF = figure;hold on
h=area(x,n_BV/sum(n_BV),'facecolor',[1 0 0]);  %%red area where the problem gonna be

legend('SCAMP')
xlabel('$$ N~[1/s]$$','Interpreter','latex','fontsize',18)
set(gca,'fontsize',14,'ygrid','on')
alpha(.5)  %%translucency of the red area

%%add new data
r3 = (b-a).*randn(1000,1) + a;
[n_BVn,xout_BVn] = histc(histazza(log10(r3)), logedg_BV);

figure(BVsurfF) 
[LEGHbv,OBJHbv,OUTHbv,OUTMbv] = legend;
P=plot(x,n_BVn/sum(n_BVn),'color','k','linewidth',2);
legend([OUTHbv;P],OUTMbv{:},'data2')

%%add new data
r4 = (b-a).*rand(1000,1) + a;
[n_BVn,xout_BVn] = histc(histazza(log10(r4)), logedg_BV);

figure(BVsurfF) 
[LEGHbv,OBJHbv,OUTHbv,OUTMbv] = legend;
P=plot(x,n_BVn/sum(n_BVn),'color','y','linewidth',2);
legend([OUTHbv;P],OUTMbv{:},'data3')




h1=gcf;
h2=figure;  
objects=allchild(h1);
copyobj(get(h1,'children'),h2);

【问题讨论】:

  • 我在使用 area 时遇到了一些与 FaceAlpha 类似的问题。尝试改用patch,它解决了我的问题。
  • 您使用的是哪个 Matlab 版本?您能否将您的代码提炼成最小的可重现命令集,我们可以直接运行而无需猜测输入?
  • @nirvana-msu 我的错。试图制作一组最小可重复的命令,我发现我发布的代码有效。问题是在那之后我执行 ``set(gca,'yscale','log')
  • @JCKaz 你的情况可能是这样吗?

标签: matlab legend figure


【解决方案1】:

我通过将原始图形的渲染器设置为“OpenGL”解决了这个问题:

set(BVsurfF,'renderer','OpenGL')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 2012-01-12
    • 1970-01-01
    • 2015-04-21
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多