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

StackOverflow https://stackoverflow.com/questions/23622802

  •  21-07-2023
  •  | 
  •  

Вопрос

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!

Это было полезно?

Решение

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!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top