Borrar filtros
Borrar filtros

change bar color based on value

30 visualizaciones (últimos 30 días)
Morgan Clendennin
Morgan Clendennin el 13 de Dic. de 2017
Respondida: Benjamin Kraus el 13 de Dic. de 2017
Hi,
I am trying to debug a code that will take two identical functions, A = randi([-100,100]) and B = randi([-100,100]), which each have their own unique values and input them into a third function, C = A - B. The code runs on a loop so it determines a value for A, B and C, and then plots the value in real time on three separate plots before moving on to the next number. I have a code that is supposed to plot the value of C on bar graph with the color green or red based on the value of C (green if C >= 0, red if C < 0). However, the code that I have ends up taking whatever the first color that was plotted and plots all bars in that color. The code that I currently have is as follows:
for i = 1:100;
A(i)=randi([-100,100]);
B(i)=randi([-100,100]);
C(i)=A(i)-B(i);
subplot(2,2,1)
plot(A,'b')
hold on
subplot(2,2,3)
plot(B,'k')
hold on
subplot(2,2,[2,4])
bar(C(C(:,1)>=0,:),'g');
hold on
bar(C(C(:,1)<0,:),'r');
hold off
drawnow
end
Any help to figure out where I'm going wrong in my code is greatly appreciated.

Respuesta aceptada

Morgan Clendennin
Morgan Clendennin el 13 de Dic. de 2017
I was able to figure out the question myself with a little less than desired solution. By creating two new variables, topC and bottomC, I was able to add in an if statement that assigned the value of C or a value of 0 to the new variables at the current index based on the criteria I was previously using for the bar graph commands. Now I just plot topC and bottomC instead and it works fine.

Más respuestas (2)

David Goodmanson
David Goodmanson el 13 de Dic. de 2017
Hi Morgan,
Unless you are trying to make a movie*, it's faster and more Matlablike to create A and B all at at once:
N = 100;
span = [-100 100];
A = randi(span,1,N);
B = randi(span,1,N);
C = A-B;
figure(1)
subplot(2,2,1)
plot(A,'b')
subplot(2,2,3)
plot(B,'k')
subplot(2,2,[2,4])
bar(C(C>=0),'g')
hold on
bar(C(C<0),'r')
hold off
I don't know exactly why the colors don't work for the for loop, but this way they do work all right.
*(there are more efficient ways to do that then replotting the entire array each time).
  2 comentarios
Morgan Clendennin
Morgan Clendennin el 13 de Dic. de 2017
Hi David,
Thank you for your quick response. I am not however looking to make the A, B and C arrays all at once. I have it set up as a loop because I want only one value for each array created at one time and plotted. Hence, why I said plotted in real time. I am only looking for a way to debug why the bar functions are not plotting as they should.
Morgan Clendennin
Morgan Clendennin el 13 de Dic. de 2017
I have modified the bar functions slightly and now I am getting a bar plot where the ever bar (including the ones already plotted) change color based on the current value.
bar(C(C(i)>=0,:),'g');
hold on
bar(C(C(i)<0,:),'r');
hold off

Iniciar sesión para comentar.


Benjamin Kraus
Benjamin Kraus el 13 de Dic. de 2017
You should check out the latest version of MATLAB. Starting in MATLAB R2017b you can specify a color for each bar in a bar series independently.
In your case, I think this will work:
b = bar(C, 'FaceColor', 'g');
b.FaceColor = 'flat';
b.CData(C<0,:) = repmat([1 0 0],sum(C<0),1);

Categorías

Más información sobre Bar Plots en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by