How can I plot these two variable in bars?

I have these two variables one of values of each month(C) and the other with the names of the corresponding values(T) I mean the names under the bars of each value of VARIABLE C. How can I plot them where we could see each other well? I attached the file with the variables. Than you in advance

 Accepted Answer

dpb
dpb on 2 May 2022
Edited: dpb on 2 May 2022
load sl % get variable titles
T=categorical(T); % turn into categorical variable
C=randi(200,1,numel(T)); % make up some data to got with...
bar(T,C)

10 Comments

That works!! Could I ask for two things more? how could I chane the color or the bars that are in position 2,4,6? And how could I place the value of the columns up of them? Thank you!
See the examples @ doc bar -- there's one for both of those plus other features as well...
Yeah I already wrote the number up the bar but I could manage to change the color
So, show the code you used -- should be able to mimic the example; we can't know what went wrong if we can't see what you tried.
b = bar(T,C,'FaceColor','flat')
for k = 1:size(C,2)
b(k).CData = k;
end
text(1:length(C),C,num2str(C'),'vert','bottom','horiz','center');
xlabel('Nombre missatges'); ylabel('Mesos')
title('Mitja de missatges de cada mes')
box off
That doesn't mimic the example -- as the doc says, when use CData, it must be a valid RGB triplet; you placed a single integer value.
You also tried to access b(k) for multiple bar handles; there's only one bar oject here. That also is NOT the syntax used in the example that changes the CData array values instead.
Try something like
hAx=gca; % retrieve the current axes handle
for k = 1:size(C,2)
b.CData(k) = hAx.ColorOrder(k,:); % set to default line color triplet in order
end
NB; Above only works as long as number of bars <= the size of the default line color array -- which is eight elements long unless user has modified factory defaults. When/if your number of bars exceeds that, you'll have to build/use a more lengthy colormap array or recycle the default colors.
I'll grant it's arcane and way more trouble than should be; I've railed to TMW about the bar interface for nearly 30 years now. It's only been in last couple they finally(!!!) did something about the bar labels; it's still way too complex to do such simple things as this and dreadfully difficult to do anything not in the pre-canned facilities. As noted to them many times, Excel has, unfortunately, passed them by in this area by lightyears in appearance and abilities. MATLAB bar graphs definitely show their age and need a total revamping. Unfortunately, TMW is hampered by the need for backward compatibility here and is stuck with a ill-guided initial design. $0.02, imo, ymmv, etc., etc., etc., ...
wow I did not know that. Thank you I will try to do it in Excel lets see if I get different colors
By default, certainly. You can set any color your monitor/graphics card can generate so it isn't that it can't be done, only that what one gets by default is not necessarily all that attractive.
I still wouldn't use Excel over MATLAB for much of anything by choice; it's just too klunky in all the other ways in which it handles data, but certainly default graphics now are generally more appearance-friendly than the default in MATLAB.
yeah but its no so much data just 6 numbers
For MATLAB, investigate colormap to learn about more and different choices and how to set/change them.
While inherently utterly flexible, the UI is, unfortunately, still based on 60s-70s era engineer-like syntax of writing code and delving into the internals of "how things work" in order to make changes. "User-friendly" and "rapid development" haven't much evolved into this arena yet if one isn't satisfied with the default colormaps supplied.
You can fiddle with Excel and see how like the color choices, then move those over to MATLAB -- what's newish and different in Excel is all the addtional visual enhancements of how can display that MATLAB just doesn't have equivalents for. Most, if not all, of these probably have more use in business graphics than scientific, but they do tend to have a more modern look and feel about them.

Sign in to comment.

More Answers (0)

Asked:

on 2 May 2022

Commented:

dpb
on 2 May 2022

Community Treasure Hunt

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

Start Hunting!