问题
I have the following code in Matlab, which produces a useful plot for me. Now I'd like to make the plot comparable color wise with another plot. For this reason, some colors should be switched: colors for...
'Pre split total EON' with 'Post split total EON'
'Pre split pure EON' with 'Post split pure EON'
'Pre split total RWE' with 'Post split total RWE'
'Pre split pure RWE' with 'Post split pure RWE'.
That is all, but I do not know how to do it, since the colors are assigned automatically...
clear all
close all
values = [4 1 11 2 3; 4 1 5 2 -10];
names = {'Pre split total EON' 'Post split total EON'...
'Pre split pure EON' 'Post split pure EON' 'Post split Uniper';...
'Pre split total RWE' 'Post split total RWE'...
'Pre split pure RWE' 'Post split pure RWE' 'PostSplitInnogy'};
categories = {'EON','RWE'};
figure;
b = bar(values,'FaceColor','flat');
ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
xticks(ticksList(:))
xticklabels([names(1,:)';names(2,:)'])
xtickangle(90)
ax1 = gca;
ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
set(ax2, 'YTick', []);
xticks(b(1).XData)
xticklabels(categories)
for k = 1:size(values,2) % for fancier colors.
b(k).CData = k;
end
回答1:
I feel like you assigned the colors manually in:
for k = 1:size(values,2) % for fancier colors.
b(k).CData = k;
end
if you just want to change the order you can do so by
b(1).Cdata = 2;
and so on. Alternativly you can change all in one with
[b.CData] = deal(2,1,4,3,5);
回答2:
I know @Finn has already answered this, but when I tested his solution, I couldn't get it to work. Strangely, I realized that even the original script was giving errors on my system. It could be that I have a very ancient version of MATLAB. But then again, I wonder if someone else also has the problem.
For some reason, I cannot set the CData
member for bar
.
Anyways, after testing, I got this to work for me.
clc
clear all
close all
values = [4 1 11 2 3; 4 1 5 2 -10];
names = {'Pre split total EON' 'Post split total EON'...
'Pre split pure EON' 'Post split pure EON' 'Post split Uniper';...
'Pre split total RWE' 'Post split total RWE'...
'Pre split pure RWE' 'Post split pure RWE' 'PostSplitInnogy'};
categories = {'EON','RWE'};
figure;
b = bar(values,'FaceColor','flat');
% only significant changes here
% ***********************************
col = ["r", "b", "y", "g", "cyan"];
for k = 1:size(values,2) % for fancier colors.
set(b(k), "FaceColor", col(k));
end
% ***********************************
ticksList = b(1).XData + arrayfun(@(x)x.XOffset, b)';
xticks(ticksList(:))
xticklabels([names(1,:)';names(2,:)'])
xtickangle(90)
ax1 = gca;
ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
set(ax2, 'YTick', []);
xticks(b(1).XData)
xticklabels(categories)
If you want to know more about how are the Colors assigned names in MATLAB, check here.
Another thing you can play around is set the colormap
as explained here.
来源:https://stackoverflow.com/questions/54071530/bar-plot-switch-colors-in-matlab