%% Control Categorical Histogram Display
% This example shows how to use histogram to effectively view categorical data. You can use the name-value pairs 'NumDisplayBins', 'DisplayOrder', and 'ShowOthers' to change the display of a categorical histogram. These options help you to better organize the data and reduce noise in the plot.
%% Create Categorical Histogram
% The sample file outages.csv contains data representing electric utility outages in the United States. The file contains six columns: Region, OutageTime, Loss, Customers, RestorationTime, and Cause.
% Read the outages.csv file as a table. Use the 'Format' option to specify the kind of data each column contains: categorical ('%C'), floating-point numeric ('%f'), or datetime ('%D'). Index into the first few rows of data to see the variables.

data_formats = '%C%D%f%f%D%C';
C = readtable('outages.csv','Format',data_formats);
first_few_rows = C(1:10,:)

%
% Plot a categorical histogram of the Cause variable. Specify an output argument to return a handle to the histogram object.

h = histogram(C.Cause);
xlabel('Cause of Outage')
ylabel('Frequency')
title('Most Common Power Outage Causes')

%
% Change the normalization of the histogram to use the 'probability' normalization, which displays the relative frequency of each outage cause.

h.Normalization = 'probability';
ylabel('Relative Frequency')

%
%% Change Display Order
% Use the 'DisplayOrder' option to sort the bins from largest to smallest.

h.DisplayOrder = 'descend';

%
%% Truncate Number of Bars Displayed
% Use the 'NumDisplayBins' option to display only three bars in the plot. The displayed probabilities no longer add to 1 since the undisplayed data is still taken into account for normalization.

h.NumDisplayBins = 3;

%
%% Summarize Excluded Data
% Use the 'ShowOthers' option to summarize all of the excluded bars, so that the displayed probabilities again add to 1.

h.ShowOthers = 'on';

%
%% Limit Normalization to Display Data
% Prior to R2017a, the histogram and histcounts functions used only binned data to calculate normalizations. This behavior meant that if some of the data ended up outside the bins, it was ignored for the purposes of normalization. However, in MATLAB® R2017a, the behavior changed to always normalize using the total number of elements in the input data. The new behavior is more intuitive, but if you prefer the old behavior, then you need to take a few special steps to limit the normalization only to the binned data.
% Instead of normalizing over all of the input data, you can limit the probability normalization to the data that is displayed in the histogram. Simply update the Data property of the histogram object to remove the other categories. The Categories property reflects the categories displayed in the histogram. Use setdiff to compare the two property values and remove any category from Data that is not in Categories. Then remove all of the resulting undefined categorical elements from the data, leaving only elements in the displayed categories.

h.ShowOthers = 'off';
cats_to_remove = setdiff(categories(h.Data),h.Categories);
h.Data = removecats(h.Data,cats_to_remove);
h.Data = rmmissing(h.Data);

% The normalization is now based only on the three remaining categories, so the three bars add to 1.
% Copyright 2012 The MathWorks, Inc.