Fork/Clone Ch.15 Data Analysis
When analyzing data, Matplotlib is a very useful python 2D plotting library which allows us to make charts and graphs. In this chapter we will again use NumPy along with the Matplotlib library. NumPy is the fundamental package for scientific computing with Python.
Instead of writing our own complicated code, we will be able to import classes, methods and functions from other libraries and use their very simple commands to easily create and manipulate various types of graphs. This chapter is the one that will make you look like a true genius and give you a huge leg up on the competition if you go to Iowa or Iowa State for engineering.
You will have to research some documentation to complete this chapter. There is an excellent YouTube video series here. that might help you figure out some things that are not included in the examples in this chapter.
Throughout this chapter, in your Jedi Training, you will first try and recreate and experiment with the examples of the four types of charts. Finally, for your Jedi Tasks you will create the four charts that are given. When you feel you've worked long enough and are ready to admit defeat, show the charts to your instructor and he will tell you whether they are good enough for you to be done. Enjoy!
First, we will try to recreate the line charts above.
#1-2: The first two lines of code below are crucial as they import the libraries that contain the functions that we need. The 'as' is used to give an easy to use alias name to the modules. You don't have to do this but if you do you can can call it anything you want. Most programmers use 'np' for the numpy library and 'plt' for the pyplot library which is a submodule of the matplotlib library.
#4-7: The four lines below are our data. We assign a list to a variable. Again you can call the variables anything you want but x and y makes sense for graphs. We are actually trying to plot three graphs on the same plot so we have several series. The last one (y3) uses the arange function from the numpy library. It will give a list of values starting with .5 and ending with 4.5 and incrementing between them by .25. Notice when calling the 'arange' function we use the library abbreviation (np) and a dot (.) before it. This tells the computer from which library to pull the function.
#28: None of the graph plotting we are about to do will work unless we call the plt.show() method so let's jump ahead and drop down to line 28 and add the following at the end of our program.
#9-10: Here is where the first two data series get plotted. We see the dot notation calling the 'plot' function from the pyplot (plt) library. The first two arguments are your x and y values. The third argument controls a lot of things. '-ro' means solid red line with circle markers. '--g^' means dashed green line with triangle markers. Finally, you can specify a label that will be placed in the legend when we create one. If you try to run your program, it will not work. Scroll down and add in line 28 so that your graph will display.
#11: In the third data series below we actually plotted a function. Notice for the x values we used the y3 series which started with .5 and finished with 4.5 and incremented by .25. For the y values instead of specifying one of our data series we actually squared our y3 series. ':bs' means dotted blue line with square points.
#12: Below we call the "fill" function from the pyplot (plt) library which will fill beneath the graph. The first two arguments are the x and y values, the third argument is a color and the alpha argument is kind of a transparency from 0-1. An alpha=0 means no color and an alpha=1 means full color.
#14-15: Below we create a legend object first and then we call the get_frame and set_facecolor methods on that object if you want to change the background color of the legend.
#17-19: Below we have three more attributes that we can set for our graph.
#21-26: Here we get fancy and actually annotate a point on your graph. Notice again the dot notation of the library followed by the function. Feel free to experiment with all of the arguments in the function to see how it changes your graph.
If you were successful in creating the line graph above, show to your instructor.
Let's recreate the bar chart above.
#1: Import pyplot.
#3-4: Create some data lists.
#6: Instead of using the numbers in the data series we can replace them with labels. Below we created a list of labels.
#7: Then we assigned them to the x values using 'xticks'. We could do the same thing with the y values and use 'yticks'
#9: Here we made a my_colors variable which contains a string of four colors which we will use to fill each bar.
#11: Instead of plt.plot we use plt.bar to make a bar graph. The first two arguments are again the x and y data series. The third argument is the bar color which we set equal to my_colors which was our string of colors. The fourth argument is the percentage with of the bar. If 1 is used the bars will run together.
#12-13: Here we set the axes labels for the graph.
#14: Here we set the title the graph.
#15: Finally, we need to show the graph.
If you were successful in creating the bar graph above, show to your instructor.
Let's recreate the Histogram above.
#1: Import pyplot.
#3-5: Create a data series.
#7-8: Create some bins to drop the test_grades into. Each bin number will be the left side of the bar
#10: This time we use plt.hist to make a histogram. The first argument is the data, the second argument are the bins. We then set the histogram type, histogram color and bar width.
#11-14: Add the x and y axes labels and the graph title and then show the histogram.
If you were successful in creating the histogram above, show to your instructor.
Let's recreate the Pie Chart above.
#1: Import pyplot.
#3: Create a list of labels for the pie chart.
#4: Create a list of relative sizes of the pieces of pie. These can be made into percents easily enough later.
#5: Corresponding colors for each slice of pie.
#6: This is how far to pull out a slice. This is normally 0 unless you want to pop out a piece for a more visually appealing look.
#8: Set aspect ratio to be equal so that pie is drawn as a circle.
#10-16: Finally we plot the chart. This time we use plt.pie. We set the arguments: sizes, explode, labels and colors to our variables above. The fifth argument, autopct will automatically print the percentages on the slices. Just use the same formatting as below. We set shadow = True to make it look better and finally we can set the start angle of our first slice to an angle from 0-360. Remember, in math, 0˚ will be directed to the right and then the slices will be added on counter-clockwise.
#18: Add the title.
#19: Show the pie chart.
If you were successful in creating the pie chart above, show to your instructor.
Ch. 15 Jedi Training
4 Charts (Line, Bar, Histogram, Pie)
Try to create this Line Chart. Hints are given in the starter code in the repository. Your graph will have different m & b values.
Try to create this Bar Chart. Hints are given in the starter code in the repository. How to make horizontal bar chart.
Try to create this Histogram. Hints are given in the starter code in the repository. Axvline Docs. Math Text Docs.
Your Pie Chart should look exactly like the following with the exception that you will use your last name.