{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Homework 06 Exercises\n", "\n", "The following exercises are based on the material from Lab 6. Feel free to open the Python notebook for [Lab 6](https://colab.research.google.com/github/AllenDowney/ElementsOfDataScience/blob/master/06_plotting.ipynb) for reference. You will need to import the `matplotlib.pyplot` library for this. You might also need `numpy`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 1:** In the last Python homework (HW05_dictionaries) we generated a dictionary containing the ages of the last 7 US Presidents (from Trump to Carter) when they took office. In this exercise, we want to visualize that data. One possible way to visualize this data is to generate a bar plot, with a bar for each President and the length of the bar indicating their age when they took office. We want to create a visualization like the one shown below.\n", "![alt text](https://raw.githubusercontent.com/stat10/DS10-Python-HW/master/Images/plot6.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**a:** Create the dictionary called `pres_age` from the HW05 to begin. Then create two lists, the first called `ages` that contains the values and the second called `names` that contains the keys of `pres_age`. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**b:** Next, generate a bar plot, using a function from `pyplot` called `bar`. Similar to `plot`, you can provide arguments to this function that determine the values for the x and y axes. In our case here, our x values are in the `names` list and the y values are in the `ages` list. Like in the Lab examples, you can suppress the result from the `bar` function by putting a semi-colon at the end. Your output should look like the image below.\n", "![alt text](https://raw.githubusercontent.com/stat10/DS10-Python-HW/master/Images/plot1.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**b:** As you should have noticed, the names along the x axis overlap, we can fix this overlap by creating a horizontal bar plot. Generate a horizontal bar plot using the function `barh`. Also, add a label to your x axis called \"Age\". Your output should look like the image below.\n", "![alt text](https://raw.githubusercontent.com/stat10/DS10-Python-HW/master/Images/plot2.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**c:** Now, to make the plot look better follow the instructions below. Remember to copy the two lines of code from the previous part (*i.e.*, the `barh` and the label for the x axis). The end result after following the instructions below should look like the image below.\n", "![alt text](https://raw.githubusercontent.com/stat10/DS10-Python-HW/master/Images/plot3.png)\n", "\n", "1. Remove the box in which the bars are displayed. Use the function from `pyplot` called `box` to do this. It takes Boolean arguments (`True`/`False`) to indicate the visibility of the box. \n", "2. Remove the tick marks from both the x and y axes. Use the function `tick_params`. You can find the documentation for this function [here](https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.tick_params). Using the documentation, figure out the values of the arguments.\n", "3. Finally, add white grid lines along the major x axis ticks. Use the function `grid`. Again, use the [documentation](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.grid.html) to figure out the arguments you need to make the end result look like the image above." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**d:** You might notice that there seems to be something in common in the \"younger\" Presidents as compared to the \"older\". The recent \"younger\" Presidents are from the Democratic Party. It would be nice to see this information in the visual. \n", "\n", "Create a list called `party_color` with strings coressponding to the color of the party (*i.e.*, \"red\" for the Republican and \"blue\" for the Democratic Presidents) of each president from Trump to Carter and use this list as an argument for `color` inside the `barh` function. Your output should look like the image below.\n", "*Note:* Copy all of the code from your previous cell and only add the `color` argument to the `barh` function.\n", "![alt text](https://raw.githubusercontent.com/stat10/DS10-Python-HW/master/Images/plot4.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**e:** The \"red\" and \"blue\" colors in the plot above are quite over-saturated. `Matplotlib` recognizes a large number of different colors as you might find in [this page](https://matplotlib.org/tutorials/colors/colors.html). One option to keep the color names human readable is to use the colors specified in the [xkcd color survey](https://xkcd.com/color/rgb/). The way to specify these colors is to prefix the color name by \"xkcd:\" (*e.g.*, 'xkcd:sky blue') in the string used to specify the color argument. \n", "\n", "Modify the `party_color` list such that the \"red\" string is now \"xkcd:light red\" and the \"blue\" string is \"xkcd:cerulean\" and re-plot the figure. The plot should look like the image below.\n", "![alt text](https://raw.githubusercontent.com/stat10/DS10-Python-HW/master/Images/plot5.png)\n", "**Bonus points to those who can modify the list using a `for` loop!**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**f:** Add a title that conveys what the plot is displaying. Also, add a legend that displays a red patch next to the word Republican and a blue patch next to Democratic. Lastly, flip the y axis so that the most recent President is at the top. The end result should look like the image below.\n", "![alt text](https://raw.githubusercontent.com/stat10/DS10-Python-HW/master/Images/plot6.png)\n", "\n", "*Hints for adding the legend (also see [this link](https://matplotlib.org/tutorials/intermediate/legend_guide.html)):*\n", "- Create patches for the legend. To create patches import `matplolib.patches` and use a function defined in it called `Patch`. In the arguments for `Patch` you can specify the color as well as the label.\n", "- Once you have created a red and a blue patch, you can call the function `legend` in `pyplot` and provide those patches as a list for the argument `handles`, for example, `plt.legend(handles=[red_patch, blue_patch])`.\n", "- To position your legend appropriately, use the arguments `bbox_to_anchor` (bounding box to anchor) and `loc` (location). `bbox_to_anchor` takes in a tuple corresponding to a normalized location in the plot frame (*e.g.*, `bbox_to_anchor=(0.5, 0.5)` is the center of the plot frame, `bbox_to_anchor=(1.5, 0.5)` is outside the frame along x and center of frame along y). `loc` specifies which point of the bounding box should be at the location specified by `bbox_to_anchor`. Play around with these two arguments to see the results and try to get a result close to the image above." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 2:** In the Python HW05, we calculated the miles per gallon of a Yamaha scooter using data that recorded the distance covered at each gas fill. Plot the miles per gallon you calculated as individual markers without a line connecting them. Also, plot a dotted line indicating the average miles per gallon you also calculated in the previous HW. Make your final plot look similar to the image below. *Note:* You can copy relevant code from the last HW.\n", "![alt text](https://raw.githubusercontent.com/stat10/DS10-Python-HW/master/Images/plot7.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution (from HW05)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution (plot)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }