%% Tall Array Plotting Examples
% This example shows several different ways you can visualize tall arrays.
%
% Create a datastore for the airlinesmall.csv data set, which contains rows of airline flight data. Select a subset of the table variables to work with and remove rows that contain missing values.

ds = tabularTextDatastore('airlinesmall.csv','TreatAsMissing','NA');
ds.SelectedVariableNames = {'Year','Month','ArrDelay','DepDelay','Origin','Dest'};
T = tall(ds);
T = rmmissing(T)

%
%% Pie Chart of Flights by Month
% Convert the numeric Month variable into a categorical variable that reflects the name of the month. Then plot a pie chart showing how many flights are in the data for each month of the year.

T.Month = categorical(T.Month,1:12,{'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'})
pie(T.Month)

%
%% Histogram of Delays
% Plot a histogram of the arrival delays for each flight in the data. Since the data has a long tail, limit the plotting area using the BinLimits name-value pair.

histogram(T.ArrDelay,'BinLimits',[-50 150])

%
%% Scatter Plot of Delays
% Plot a scatter plot of arrival and departure delays. You can expect a strong correlation between these variables since flights that leave late are also likely to arrive late.
% When operating on tall arrays, the plot, scatter, and binscatter functions plot the data in iterations, progressively adding to the plot as more data is read. During the updates the top of the plot has a progress indicator showing how much data has been plotted. Zooming and panning is supported during the updates before the plot is complete.

scatter(T.ArrDelay,T.DepDelay)
xlabel('Arrival Delay')
ylabel('Departure Delay')
xlim([-140 1000])
ylim([-140 1000])

%
% The progress bar also includes a Pause/Resume button. Use the button to stop the plot updates early once enough data is displayed.
%
%% Fit Trend Line
% Use the polyfit and polyval functions to overlay a linear trend line on the plot of arrival and departure delays.

hold on
p = polyfit(T.ArrDelay,T.DepDelay,1);
x = sort(T.ArrDelay,1);
yp = polyval(p,x);
plot(x,yp,'r-')
hold off

%
%% Visualize Density
% The scatter plot of points is helpful up to a certain point, but it can be hard to decipher information from the plot if the points overlap extensively. In that case, it helps to visualize the density of points in the plot to spot trends.
% Use the binscatter function to visualize the density of points in the plot of arrival and departure delays.

binscatter(T.ArrDelay,T.DepDelay,'XLimits',[-100 1000],'YLimits',[-100 1000])
xlim([-100 1000])
ylim([-100 1000])
xlabel('Arrival Delay')
ylabel('Departure Delay')

%
% Adjust the CLim property of the axes so that all bin values greater than 150 are colored the same. This prevents a few bins with very large values from dominating the plot.

ax = gca;
ax.CLim = [0 150];

% Copyright 2019 The MathWorks, Inc.