Presentation is loading. Please wait.

Presentation is loading. Please wait.

MBAC 611.  Within your private mbac611 folder create a lab5 folder.  Copy the Moodle file lab3_vars to your lab5 folder.  Start Mathematica  Enter.

Similar presentations


Presentation on theme: "MBAC 611.  Within your private mbac611 folder create a lab5 folder.  Copy the Moodle file lab3_vars to your lab5 folder.  Start Mathematica  Enter."— Presentation transcript:

1 MBAC 611

2  Within your private mbac611 folder create a lab5 folder.  Copy the Moodle file lab3_vars to your lab5 folder.  Start Mathematica  Enter SetDirectory[“h:mbac611/lab5”] into the Mathematica notebook and press the evaluation key.

3  In an earlier lab we saved the fares, header and fieldTotal variables values to disk so that they could be used in this lab.  To restore these values enter the following expression into the Notebook: <<lab3_vars

4  It would be nice to visualize some of the data.  One of the most basic forms of visualization is the bar chart. It is created using the BarChart function.  Enter the following expression: BarChart[fieldTotal]

5 The following output should appear: Note that, in the Notebook, you can move your mouse over each bar to discover the number the bar represents. This is not a particularly useful bar chart. In fact, we have attempted to graph too much data. We included the first two fields of fieldTotal which were intentionally left zero. Also we included the last field (26 th ) of fieldTotal which includes the total of all metrocard use. Also we don’t know what have any labels on the x-axis so it is not possible to easily identify what stations the bars correspond to.

6 In our next bar chart we will only graph elements 3 through 25 of the fieldTotal list. This way we will only be graphing unique metrocard totals. Enter the following expression into the Notebook: BarChart[fieldTotal[[3;;25]],ChartLabels->header[[3;;25]]] You should see the following output: This graph is only slightly better – it graphs the correct data but the axis is unreadable because the metrocard type names are running into eachother.

7 Select elements 3 through 25 of the fieldTotal list. ChartLabels is an option of the BarChart function that allows you to label the elements of the chart. This symbol is the Mathematica transformation symbol. It is formed by typing the characters -> without an intervening space. It essentially used to temporarily make an assignment. In this case we are temporarily assigning to ChartLabels. We are assigning the 3 rd through 25 th header list elements to ChartLabels.

8  We can fix the readability of the bar graph by adding the BarOrigin option.  Enter the following expression: BarChart[fieldTotal[[3;;25]],ChartLabels->header[[3;;25]], BarOrigin->Left] The BarOrigin option allows you to specify the origin of the bars. Other possible BarOrigin assignments include Right, Top and Bottom.

9 The following output should appear: This looks better. We can even improve on it by enlarging the graph. To enlarge the graph simply click once on the graph and then click and hold the mouse button down on one of the resize boxes. Then move the mouse. Try enlarging the graph using the bottom resize box on the right-hand side.

10 This graph is much clearer. We can see that Full Fare (FF) metrocards are the most popular, with 30-Day-Unlimited (30-D UNL) and 7-Day- Unlimited (7-D UNL) coming in second and third.

11  Mathematica has many additional options that can be added to the BarChart function.  From the Lab5 Moodle folder download the metrocard.jpg image and place a copy in your private mbac611/lab5 folder.  Enter the following expression: metroCardImage=Import["metrocard.jpg"]

12 Enter the following expression: BarChart[fieldTotal[[3;;25]],ChartLabels->header[[3;;25]], BarOrigin->Left, ChartElements->metroCardImage] Note the additional option (ChartElements). You should see the following output:

13  Create a bar chart based on the first (1 st ) station (record) in the fares list. Remember that the station metrocard data is in fields 3-25 (Hint: fares[[1,3;;25]])  The labels for the bar chart are the same as our previous chart  Set the BarOrigin option should to Left.  Add a chart title by adding the PlotLabel option. PlotLabel should be assigned the second (2 nd ) element in the first (1 st )record – the station name is stored there.  It is not necessary to use the metrocard image for the bars, but are free to use it if you wish.

14 The output should appear as follows (note the station name):

15  We can view the bar charts for each station by executing the BarChart function on every member of the fares list.  However, a much better way of viewing the charts is to use the Manipulate function.  The Manipulate function allows us to change arguments to functions graphically, using a sliding bar.

16 Enter the following expression: Manipulate[BarChart[fares[[n,3;;25]], ChartLabels->header[[3;;25]], BarOrigin->Left, PlotLabel->fares[[n,2]]], {n,1,Length[fares]}] The following output should appear: Note the slider. Click and hold the mouse button on the slider. Then move the slider to the right. The station bar charts will change. Note that you can still resize the chart if it is too small.

17 Click on the expand box at the end of the slider.

18 Click once on the button. Notice that the value in the textbox jumps from 1 to 20. It appears that Mathematica is jumping to the next multiple of 20 every time we click the button. A similar action happens when we click the button, but the number decreases in multiples of 20.

19 Manipulate[ BarChart[ fares[[n,3;;25]], ChartLabels->header[[3;;25]], BarOrigin->Left, PlotLabel->fares[[n,2]] ], {n,1,Length[fares]} ] Lets take a closer look at the function code. Note that spaces between arguments, options or functions are ignored by Mathematica so it is fine to add space for clarity. The first argument of the Manipulate function is the function we are going to visualize. In this case it is the BarChart function. Note that value we are going to be changing with the slider is replaced with the variable n. The second argument is the iterator. This is very similar to the iterator that appeared in the Do function. The variable n should, depending on the slider, take on values between 1 and the length of the fares list – which is the number of stations (465).

20  We can fix the button increment and decrement problem by telling the Manipulate function to change the value of n by 1. This is done by adding an increment value (one in this case) as the fourth member of the iterator - {n,1,Length[fares],1}  Notice that our variable (n) appears next to the slider. This indicates that the user is controlling this variable with the slider. However, n is not particularly meaningful. Change the variable from n to station. Remember, if you change it in the iterator you also need to change it in the BarChart function call.  Make the two changes described above and execute the Manipulate function call again.

21 Your output should look similar to the following: Note the variable name n has been replaced by the variable name station. When you press the and buttons the increment and decrement step should now be 1.

22  Save a copy of your notebook in your lab5 folder.  Submit the notebook file to Moodle.


Download ppt "MBAC 611.  Within your private mbac611 folder create a lab5 folder.  Copy the Moodle file lab3_vars to your lab5 folder.  Start Mathematica  Enter."

Similar presentations


Ads by Google