Change the color of each bar in the Bar graph of 3 arrays
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
NETHRAVATHI S
el 25 de Abr. de 2021
Comentada: Scott MacKenzie
el 25 de Abr. de 2021
Hi,
I am getting a bar graph like this with one color.
I need same color for the first element of each array.. My code is like this
encomb=[4 3 2 1;5 4 3 2; 6 5 4 3 ];
figure(12)
hold on
bb=bar(encomb(:))
xlabel('type of consumer')
ylabel('energy consumed kWh/month')
title('Energy consumed for different case studies')
hold off
0 comentarios
Respuesta aceptada
Scott MacKenzie
el 25 de Abr. de 2021
Try this, but with your own colors in clr:
clr = rand(12,3);
encomb=[4 3 2 1;5 4 3 2; 6 5 4 3 ];
figure(12)
hold on
bb=bar(encomb(:), 'facecolor', 'flat');
bb.CData = clr;
xlabel('type of consumer')
ylabel('energy consumed kWh/month')
title('Energy consumed for different case studies')
hold off
Output:
0 comentarios
Más respuestas (1)
NETHRAVATHI S
el 25 de Abr. de 2021
1 comentario
Scott MacKenzie
el 25 de Abr. de 2021
Perhaps I misunderstood your original question. The issue maybe that you "ungrouped" the data by passing encomb(:) into the bar function. If you pass in encomb instead, the data are treated as groups, one group per row. You'll get different colors for each bar within a group, but the same color for bar positions between groups:
encomb=[4 3 2 1;5 4 3 2; 6 5 4 3 ];
figure(12)
hold on
bb=bar(encomb, 'facecolor', 'flat'); % NOTE: encomb, not encomb(:)
xlabel('type of consumer')
ylabel('energy consumed kWh/month')
title('Energy consumed for different case studies')
hold off
Here, you are getting MATLAB's default colors for a bar chart with grouped data. Is this what you want?
Ver también
Categorías
Más información sobre Data Exploration 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!