Set the transparency of bars in a bar plot and set the y-axis to a log scale - but both don't seem to work in MATLAB

喜你入骨 提交于 2019-12-08 17:34:15

问题


In MATLAB, I want to set the transparency of bars in a bar plot to 0.3 and set the y-axis to a log scale - but both don't seem to work...

subplot('Position',[0.15 0.7 0.45 0.15]);
data = [1 2 5 4 7 4 1];
B = bar(data,'g');
ch = get(B,'child');
set(ch,'facea',.3)

That works fine, but then add on this:

set(gca,'YScale','log');

and the transparency setting doesn't work. Any ideas? Thanks!


回答1:


Log Scale axis and Transparency do not work together in Matlab

Why?

OpenGl renderer, (which must be used for the transparency) does not support logarithmic axis - this is apparently in the documentation as of 2010b and is also mentioned here

Solution

Mimic Log axis by transforming data & setting Yaxis ticks

the code below makes the bar plot with log axis, gets the required properties, clears the axis and then uses the information to mimic the log axis
code:

subplot('Position',[0.15 0.7 0.45 0.15]);
data = [1 2 5 4 7 4 1];
B = bar(data,'g');
set(gca,'Yscale','log')
ticks=get(gca,'Ytick');
ticklabel=str2num(get(gca,'YtickLabel'));
set(gca,'Yscale','linear')
cla

B = bar(log(data),'g');
set(gca,'Ytick',log(ticks));set(gca,'YtickLabel',10.^ticklabel)
ch = get(B,'child');
set(ch,'facea',.3)

The only loss is the formatting of tick labels.
Finally I cant guarantee anything when applied to negative data, although it will almost certainly throw a warning and not work properly!



来源:https://stackoverflow.com/questions/23622802/set-the-transparency-of-bars-in-a-bar-plot-and-set-the-y-axis-to-a-log-scale-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!