## Create Bar Charts, Histograms, and Pie Charts

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Bar charts, histograms, and pie charts help you compare different data samples, categorize data, and see the distribution of data values across a sample. These types of plots are very useful for communicating results of data analysis. Bar charts, histograms, and pie charts can help your audience understand your ideas, results, and conclusions quickly and clearly.

### Bar Charts

To compare different data samples or to show how individual elements contribute to an aggregate amount, use bar charts. A bar chart represents each element of a data sample as one bar. Bars are distributed along the horizontal or vertical axis, with each data element at a different location. To compare data samples, create a bar chart for two or more data samples. In this case, MuPAD® accesses elements with the same index and plots the bars for these elements next to each other. For example, create three lists of random numbers:

```x := [frandom() \$ i = 1..10]; y := [frandom() \$ i = 1..10]; z := [frandom() \$ i = 1..10]```
` `
` `
` `

To create a 2-D bar chart, use the `plot::Bars2d` function. The chart displays data from the data samples `x`, `y`, and `z`. The resulting plot shows the elements with the same index clustered together. Small gaps separate each group of elements from the previous and the next group:

`plot(plot::Bars2d(x, y, z))` To create a 3-D bar chart, use the `plot::Bars3d` function. This function accepts matrices and arrays. The function also accepts nested lists with flat inner lists. The `plot::Bars3d` function draws each element as a separate 3-D block. The elements of each row of an array or a matrix (or the elements of each flat list) appear along one horizontal axis. Bars that represent elements in the first column of an array or a matrix appear along the other horizontal axis. If you use a nested list, the elements of the inner lists with the same indices appear along the other horizontal axis. By default, the `plot::Bars3d` function does not display gaps between the groups of elements. Use the `Gap` option to create gaps and specify their size:

`plot(plot::Bars3d([x, y, z], Gap = [0.5, 0.8]))` ### Histograms

Histograms show the distribution of data values across a data range. They divide the data range into a certain number of intervals (bins), tabulate the number of values that fall into each bin, and plot these numbers using bars of varying height. To create a histogram, use the `plot::Histogram2d` function. By default, this function divides the data range into seven bins. To specify the number of bins, use the `Cells` option. For example, create the histogram of the following data sample categorizing the data into 10 bins:

```data := [-10.1, -1, 1.1, 3.5, 13, 0, -5.5, 0.5, 7.9, 15, 0.15, 6.7, 2, 9]: plot(plot::Histogram2d(data, Cells = 10))``` ### Pie Charts

Pie charts can help you effectively communicate a portion (or percentage) that each element of a data sample contributes to the total number of all elements. To create a 2-D pie chart, use the `plot::Piechart2d` function. To create a 3-D pie chart, use the `plot::Piechart3d` function. A 3-D pie chart does not show any additional information. The 3-D view simply adds depth to the presentation by plotting the chart on top of a cylindrical base and lets you rotate the plot.

Suppose, you need to analyze the following list of numbers:

```data := [-10.1, -1, 1.1, 3.5, 13, 0, -5.5, 0.5, 7.9, 15, 0.15, 6.7, 2, 9]:```

First, use the `stats::frequency` function to categorize the data into bins. (See Data Binning for more details.)

`T := stats::frequency(data)`
` `

The result is a table that shows the intervals (bins), number of elements in those bins, and the data elements in each bin. The `plot::Piechart2d` and `plot::Piechart3d` functions do not accept tables as arguments. They accept lists, vectors, and arrays with one row or one column. Before creating a pie chart, extract the bins and the number of elements in them into two separate tables:

```Counts := map(T, op, 2); Bins := map(T, op, 1)```
` `
` `

Now, extract the entries from the `Bins` and `Counts` tables and create the lists containing these entries:

```slices := [Counts[i] \$ i = 1..10]: titles := [expr2text(Bins[i]) \$ i = 1..10]:```

Create a 2-D pie chart by using the `plot::Piechart2d` function. The `slices` list specifies the portions that each bin contributes to the total number of all elements of the data sample. The `titles` list specifies the titles for each piece on the pie chart:

`plot(plot::Piechart2d(slices, Titles = titles))` Create a 3-D pie chart from the same data by using the `plot::Piechart3d` function. To rotate the resulting 3-D chart, click any place on the chart, hold the mouse button and move the cursor:

`plot(plot::Piechart3d(slices, Titles = titles, Radius = 0.3))` #### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos