Too many x labels on bar graph

14 views (last 30 days)
Harley Koltai
Harley Koltai on 27 Sep 2022
Edited: Siddharth Bhutiya on 3 Oct 2022
Hi,
I'm trying to plot a categorical array against a respective double, but have filtered the indexes for values > 2. I.e., assume we have categorical array 'x', with the respective y-axis values 'y'. This ultimately cuts-down the size of the arrays from 100x1, to 30x1.
yindx = y>3;
x = x(yindx);
y = y(yindx);
The resultant filtered arrays look completely correct. However, when I plot it into a bar graph 'bar(x,y)', the x-labels for the filtered values are still present, but the corresponding y-values aren't plotted. So I can see all 100 original x-labels, but only the 30 filtered y-value bars.
How can I remove the unwanted x-labels from the graph, to only leave the corresponding 30 labels on the x-axis?
  2 Comments
Chunru
Chunru on 27 Sep 2022
Can you include some sample data for x and y?

Sign in to comment.

Answers (2)

Bala Tripura Bodapati
Bala Tripura Bodapati on 30 Sep 2022
Hi Harley
It is my understanding that after filtering the categorical array and plotting the values, all the categories are displayed on the bar graph instead of the filtered categories.
On creating a categorical array, all the unique categories get stored in the assigned variable. On filtering this array using indexing, the array values will be filtered but the categories stored will not be affected. This can be verified by executing the 'categories' function on the filtered array.
To display only the filtered categories on the bar graph, the following workarounds can be considered:
1. Each element of the categorical array is unique: Remove the categories not present in the filtered array using removecats function. The following code illustrates this use-case:
x=["a","b","c","d","e","f"]
x=categorical(x)
y=[1,2,3,5,6,8]
yindx=y>3
x_unfiltered_categories=string(x(~yindx))
x=x(yindx)
y=y(yindx)
x=removecats(x,x_unfiltered_categories)
bar(x,y)
2. Each element of the categorical array is not unique: Convert the filtered categorical array to string and then back to categorical. The following code illustrates this use-case:
x=["a","b","c","a","b","f"]
x=categorical(x)
y=[1,2,3,5,6,8]
yindx=y>3
x=x(yindx)
y=y(yindx)
x=categorical(string(x))
bar(x,y)

Siddharth Bhutiya
Siddharth Bhutiya on 3 Oct 2022
Edited: Siddharth Bhutiya on 3 Oct 2022
When plotting the categorical it will plot all the categories that were defined when creating the categorical. If you want the plots to not show the categories not present in the new subset then call removecats before plotting. Calling removecats with just one input argument will remove any unused categories from your categorical array.
>> x = categorical(1:10);
>> y = x(4:8);
>> bar(y,4:8) % Plots all categories 1 2 .... 9 10
>> y = removecats(y);
>> bar(y,4:8) % Only plots 4 5 ... 7 8

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!