Clear Filters
Clear Filters

Bar graphing extra categorical info

3 views (last 30 days)
I am trying to plot a bar graph of the "worst 5" of something. I am revamping some code from before. Before, I used c=categorical(names of things) then c(Position of worst 5) to graph and it worked. It showed only the 5 from the list. I am trying the same thing again, but it keeps showing the whole list. I have tried setting D=c(Bad Pos) and using that in bar, I have tried clearing the c variable while doing that as well. No matter what, it keeps graphing everything.
WHY=c(BadPosW,1);
WorstSpeeds=Speedsa(BadPosW,:)
figure(1)
subplot(2,2,2)
Worst5Plot=bar(WHY, [WorstSpeeds(1:5,4) WorstSpeeds(1:5,5) WorstSpeeds(1:5,6)])
As of right now, thats the last thing I have tried.
bar(WHY, [WorstSpeeds(:,4) WorstSpeeds(:,5) WorstSpeeds(:,6)])
Above is what I am entering and is still giving extra data. I saved the original c, the cut c as WHY and the speeds. I tried loading only WHY and WorstSpeeds, still giving me all the other data somehow.
  2 Comments
dpb
dpb on 26 Mar 2019
We'd have to see a minimum working example including data to be able to diagnose the issue.
Calvin Ebert
Calvin Ebert on 27 Mar 2019
I have updated with the variables from the workspace

Sign in to comment.

Accepted Answer

Calvin Ebert
Calvin Ebert on 27 Mar 2019
did WHY=string(WHY)
then WHY=categorical(WHY) and it worked. Still dont understand what was going on to cause it to pull all the values and would be curious if anyone has an explanation but that solved it.
  4 Comments
dpb
dpb on 27 Mar 2019
Edited: dpb on 28 Mar 2019
Sure, except it really wouldn't be two lines but a built string...another Answer illustrated this just a day or so ago for a text object but the same "trick" works for tick labels...
Of course, you quickly run out of room on axes...
NB: you can, of course, also build those text strings dynamically an example of which was in last comment at
That was in a loop for "one-at-a-time" but for tick labels it's more handy to use the vectorized ability of num2str instead...one caveat there is you must use a column vector for the input arrays or the output cell strings will be run together horizontally.
dpb
dpb on 28 Mar 2019
I did submit a SRQ for consideration of ths behavior in the categorical axis object. PLOT() behaves identically, not surprisingly.
We'll see what TMW has to say... :)

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 28 Mar 2019
Try just calling histogram. First let's build some categorical data:
C = ["apple"; "banana"; "cherry"; "strawberry"; "kiwi"; "watermelon"];
V = randi(numel(C), 1000, 1);
catData = categorical(C(V));
Next let's show the histogram containing all the data.
figure;
h = histogram(catData);
Finally let's show just the three largest bins.
figure
h = histogram(catData, 'NumDisplayBins', 3, 'DisplayOrder', 'descend');
There are other options that may be of use to you, like 'ShowOthers' if you want to see one catch-all bin for everything that's not in the top 'NumDisplayBins' bins or if you already have the 'BinCounts' and want to specify those as the values to use when creating the bars for the 'Categories'. See the documentation for histogram for more information on the options to which I referred.
  1 Comment
dpb
dpb on 28 Mar 2019
That's kewl and some new features in histogram I wasn't aware of, Steven.
I think the issue w/ bar and plot is still real, however, as (at least OTOMH) I don't see how to build the grouped bar plot with histogram as his data are 2D, not just a vector.

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!