# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment
responses = initialize_assignment("2_refining_plot_q", "week_8", "lecturenotgraded", assignment_points = 44.0, assignment_tag = 'week8-lecturenotgraded')
# Initialize Otter
import otter
grader = otter.Notebook("2_refining_plot_q.ipynb")
๐ป Activity: Refining a Simple Plot#
In many labs and classes, youโll need to meaningfully visualize data for your audience.
Successful communication of data is one of the most important aspects of engineering. If you canโt get important ideas out of your head in a way that makes them digestible to others then you are unlikely to succeed as an engineer.
Please follow along on your computers as we refine a visualization of sine and cosine.
Create data#
The first step is to get data for the sine and cosine functions.
Note that we import pyplot
from matplotlib
to use pyplot
to visualize 2-D data.
import numpy as np
import matplotlib.pyplot as plt
# creates a linear spaced array
# create a linear spaced array of 256 values ranging from -ฯ to +ฯ (included), using the np.linspace() function, set the endpoint to True
# assign the result to the variable X
...
# computes the sine and cosine
# compute the cosine and sine of the array X, and assign the results to the variables C and S
...
grader.check("q1-create-data")
X
is now a NumPy array with 256 values ranging from -ฯ to +ฯ (included). C
is the cosine (256 values), and S
is the sine (256 values).
# plot the cosine against X
# use the plt.plot() function to plot the cosine against X
# save as the variable plt_cosine
...
# plot the sine against X
# use the plt.plot() function to plot the sine against X
# save as the variable plt_sine
...
# display the plot using the plt.show() function
...
grader.check("q2-plot-data")
Question 2 (Points: 2.0): Customizing Plots#
Matplotlib was designed to make 2-dimensional plotting easy! We will generate simple graphics today, but you can use these links to learn about more powerful applications of matplotlib:
Letโs adjust color so cosine is blue and the sine is red. Letโs also increase line width to 2.5.
We want the graphic to display well on a laptop screen, so letโs also adjust the aspect ratio (width:height) using figsize
.
# create a figure with a size of 10x6 and a dpi of 300
# save as the variable plt_figure
# BEGIN SOLUTION
plt_figure = plt.figure(figsize=(10, 6), dpi=300)
# END SOLUTION
# plot the cosine against X
# use the plt.plot() function to plot the cosine against X
# set the color to blue, the linewidth to 2.5, and the linestyle to "-", this will make a solid blue line
# save as the variable plt_cosine
# BEGIN SOLUTION
plt_cosine = plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
# END SOLUTION
# plot the sine against X
# use the plt.plot() function to plot the sine against X
# set the color to red, the linewidth to 2.5, and the linestyle to "-", this will make a solid red line
# save as the variable plt_sine
# BEGIN SOLUTION
plt_sine = plt.plot(X, S, color="red", linewidth=2.5, linestyle="-")
# END SOLUTION
grader.check("q3-create-figure")
Question 3 (Points: 2.0): Setting Limits#
The current limits of the figure are a bit too tight. To better visualize the functions we want to extend the range of the x- and y-axes.
Extend the ranges to be 10% more negative at the minimum and 10% greater at the maximum.
# create a new figure with a size of 8x5 and a dpi of 300
# save as the variable plt_figure
...
# create a subplot with 1 row and 1 column, and the first plot, this is done by plt.subplot(111)
...
# create a linear spaced array of 256 values ranging from -ฯ to +ฯ (included), using the np.linspace() function, set the endpoint to True
# assign the result to the variable X
...
# compute the cosine and sine of the array X, and assign the results to the variables C and S
...
# copy these lines from above
# use the plt.plot() function to plot the cosine against X
# set the color to blue, the linewidth to 2.5, and the linestyle to "-", this will make a solid blue line
...
# use the plt.plot() function to plot the sine against X
# set the color to red, the linewidth to 2.5, and the linestyle to "-", this will make a solid red line
...
# set the limits of the x-axis and y-axis
# set the range of the x-axis to be 10% more negative at the minimum and 10% greater at the maximum, hint use X.min() and X.max()
# set the range of the y-axis to be 10% more negative at the minimum and 10% greater at the maximum, hint use C.min() and C.max()
# save as the variable plt_xlim and plt_ylim
...
# display the plot using the plt.show() function
...
grader.check("q4-set-limits")
Question 4 (Points: 15.0): Setting Ticks#
The current ticks are not ideal because they do not show the interesting values for sine and cosine, like ยฑ ฯ and ยฑ (ฯ/2).
Use the xticks
function to make ticks at -ฯ, -ฯ/2, 0, ฯ/2, and ฯ.
Use the yticks
function to make ticks at -1, 0, and 1.
Setting the Tick Labels#
The ticks are now properly placed, but their label is not very explicit.
We could guess that 3.142 is ฯ, but it would be better to make it explicit.
When we set tick values, we can also provide a corresponding label in the second argument list.
Note that weโll use latex to allow for a nice rendering of the label.
# Copy the cell above into the area with the ..., this will create another version of the same plot
...
# use the plt.xticks() function to make ticks at -ฯ, -ฯ/2, 0, ฯ/2, and ฯ, this is done by providing a list of values to the function
# use the plt.yticks() function to make ticks at -1, 0, and 1, this is done by providing a list of values to the function
...
# display the plot using the plt.show() function
...
grader.check("q5-set-ticks")
Moving Spines#
Spines are the lines connecting the axis tick marks and noting the boundaries of the data area.
They can be placed at arbitrary positions. Until now, they were on the border of the axis.
Weโll change that since we want to have them in the middle.
Since there are four of them (top/bottom/left/right), weโll discard the top and right by setting their color to none.
Weโll move the bottom and left ones to the origin of the graph.
# Copy the cell above into the area with the ..., this will create another version of the same plot
...
# set the right and top spines to none
...
# set the bottom spine to the origin where the data is 0
# move the bottom spline to the origin
...
grader.check("q6-move-spines")
Question 6 (Points: 6.0): Adding a Legend#
Legends help someone viewing a plot to understand what it shows.
Letโs add a legend in the upper left corner.
Insert the keyword argument label
to the plot commands, like label="cosine"
# Copy the cell above into the area with the ..., this will create another version of the same plot
...
# call the plt.legend() function to add a legend in the upper left corner, turn the frame off, by setting frameon to False
...
# display the plot using the plt.show() function
plt.show()
grader.check("q7-add-legend")
Submitting Assignment#
Please run the following block of code using shift + enter
to submit your assignment, you should see your score.
from pykubegrader.submit.submit_assignment import submit_assignment
submit_assignment("week8-lecturenotgraded", "2_refining_plot_q")