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 commentaires

dpb
dpb le 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 le 27 Mar 2019
I have updated with the variables from the workspace

Connectez-vous pour commenter.

 Réponse acceptée

Calvin Ebert
Calvin Ebert le 27 Mar 2019

0 votes

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 commentaires

dpb
dpb le 27 Mar 2019
Modifié(e) : dpb le 28 Mar 2019
The issue is that when subsetting a categorical array, more of the parent data of the parent categorical type comes along with the subset variable than one might expect--note that all the original category names are still associated with their rank in the axis labels.
I was unaware of that "quirk", too; generally it doesn't show up but with the plotting routine the categorical axis appears to interpret that hidden data and use it to set the limits. I'd consider this a poor "quality of implementation" decision even if TMW won't call it a bug. I do believe the behavior deserves a service request for the unexpected result.
You can see what's going and why your redefinition of the categorical variable fixes the problem by the following--
>> [double(WHY) double(categorical(string(WHY)))]
ans =
32 5
11 1
13 2
24 3
25 4
>>
You see that underneath the hood, the categorical variable is just a coded integer whose value is its position in the hierarchy. Your subset happens to span a large range and the axis object plots them on the axis by the hierarchy, not just as sequential 1:numel() with the associated name as one would expect.
You can also see how this comes about by
>> find(ismember(c,WHY))
ans =
11
13
24
25
32
>>
so that you can find the "bad" vector out of the original categorical array from the small subset because it retains its same value associated with the original. This part is necessary to keep the correlation and explains why that data "hangs around" in the subarray, but the axis routine isn't smart enough to only use that subset.
One workaround besides yours above would be
bar(WorstSpeeds(:,4:6))
hAx=gca;
hAx.XTickLabel=cellstr(WHY);
Then the axis isn't categorical variable but you have the right names for label.
Calvin Ebert
Calvin Ebert le 27 Mar 2019
that is very informitive thank you! Do you know if it would be possible to have an xticklabel with 2 lines? like {'Machine1':'MachineType'}?
dpb
dpb le 27 Mar 2019
Modifié(e) : dpb le 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 le 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... :)

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 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 commentaire

dpb
dpb le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Distribution Plots dans Centre d'aide et File Exchange

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by