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
Asked Answered
F

1

7

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!

Furan answered 13/5, 2014 at 4:26 Comment(2)
I'm almost convinced this is a bug.Friseur
@Trilarion more a limitation than a bug see hereBarbecue
B
6

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!

Barbecue answered 13/5, 2014 at 9:54 Comment(2)
Thanks @RTL! What if I have x-data and y-data? For example, B = bar(log(datax,datay),'g') doesn't work... Thanks again!Furan
oops, my bad. This works: B = bar(x,(log(y)),'g'). Thanks again.Furan

© 2022 - 2024 — McMap. All rights reserved.