Bar graphing extra categorical info

2 次查看(过去 30 天)
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 个评论
dpb
dpb 2019-3-26
We'd have to see a minimum working example including data to be able to diagnose the issue.
Calvin Ebert
Calvin Ebert 2019-3-27
I have updated with the variables from the workspace

请先登录,再进行评论。

采纳的回答

Calvin Ebert
Calvin Ebert 2019-3-27
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 个评论
dpb
dpb 2019-3-27
编辑:dpb 2019-3-28
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 2019-3-28
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... :)

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2019-3-28
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 个评论
dpb
dpb 2019-3-28
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by