Is there a better way to plot categorical data radially than using the polarhistogram function?
Show older comments
It is easy to plot categorical data with the histogram function:
authors = categorical({'Austen','Bradbury','Brown','Brown','Brown','Brown','Capote','Faulkner','Faulkner','Fitzgerald','Fitzgerald','Hawthorne','Heller','Hemingway','Hemingway','Hemingway','Irving','James','King','King','King','King','King','King','King','Lee','Lee','London','Melville','Miller','Miller','Morisson','O''Brien','Rand','Steinbeck','Steinbeck','Steinbeck','Twain','Twain','Twain','Twain','Twain','Updike','Updike','Updike','White'});
figure
histogram(authors,DisplayOrder='descend',Normalization='probability')
But, this does not work if you try to plot the data radially using polarhistogram, which requires that the data be numeric or logical. It is simple enough to get the number of counts for each cateogory in the categorical data by using the histcounts function, but plugging the numeric data directly into polarhistogram will not treat the numbers as discrete values. Instead, the function will automatically bin the data like this:
num_authors=histcounts(authors);
figure
polarhistogram(num_authors)
I have found a convoluted workaround in which you can specify the number of bins that you want the data to be divided into, but this is a pain because you have to define the widths of the bins in radians and the distance between tick marks in degrees:
% Get a list of all the category names
author_cats = categories(authors);
% Define number of bins
nbins = length(author_cats);
% Define widths of the bins (in radians)
binwidths = deg2rad(linspace(0,360,nbins+1));
% Define position of tick marks for adding categorical labels (in degrees)
center_bin_positions = (360/nbins) / 2;
bintickpositions = center_bin_positions : center_bin_positions *2 : 360 - center_bin_positions;
% Create plot
figure
h = polarhistogram('BinEdges',binwidths,'BinCounts',num_authors);
ax = gca;
% Normalize inner cicular tick marks as percentages
h.Normalization = 'percentage';
ax.RTickLabel = string(ax.RTickLabel) + '%';
% Adjust categorical tick label positions and add labels
thetaticks(bintickpositions)
thetaticklabels(author_cats)
for i = 1:nbins
value = sprintf('%0.1f',h.Values(i));
ax.ThetaTickLabel{i} = string(ax.ThetaTickLabel{i}) + ' (' + value + ' %)';
end
While this seems to work okay, making further customizations to the plot only makes things more complicated. For instance, I would prefer to plot the categorical data in descending order so that everything can be more easily compared.
Is there a better way to produce plots like this in MATLAB?
2 Comments
Mario Malic
on 16 Jan 2024
What's wrong with histogram, you can adjust the looks of it, make bins wider and align text to be perpendicular to the X axis which should look just fine. Polar charts are a little bit complicated, maybe I would suggest to do it in Excel if you insist on that.
Austin M. Weber
on 16 Jan 2024
Edited: Austin M. Weber
on 16 Jan 2024
Accepted Answer
More Answers (0)
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


