More Visualization CT @ VT Why Functions How can code be made - - PowerPoint PPT Presentation
More Visualization CT @ VT Why Functions How can code be made - - PowerPoint PPT Presentation
Introduction to Computational Thinking More Visualization CT @ VT Why Functions How can code be made available for others to use? Cut/paste of code (as we did for Blockly Python) works only for small amounts of code Need better
CT@VT Why Functions
How can code be made available for others to
use?
Cut/paste of code (as we did for Blockly Python)
works only for small amounts of code
Need better ways to organize and reuse large
amounts of code
Python provides three ways
Function – code that performs a single action
Simple action: round a number
Complex action: generate a visualization
Module – a collection of related functions
e.g., all of visualization functions
Package – a collection of related modules
e.g., all of the modules that do different forms of visualization
Slide 2 Fall 201 2015
CT@VT Packages and Modules
Matplotlib: commonly used in Python to create visualizations (see http://matplotlib.org/gallery.html).
Syntax: matplotlib.pyplot.show()
Shorthand: import matplotlib.pyplot as plt ... plt.show()
Slide 3 Fall 201 2015
package module matplotlib pyplot plot show functions
CT@VT Functions in 3 easy steps
Step 1: functions have names
show() # show the visualization
Step 2: functions may have parameters
plot(data) # plot the list data
Step 3: functions may return a value
val = sqrt(number) # square root
Slide 4 Fall 201 2015
Notes
The user documentation tells you what a function does and what you need to use it
You do not need to know how a function is implemented to use it (and you often don’t care)
Reusing functions is a highly valued professional practice
CT@VT Three simple visualizations Name Function Name Typical Usage Line plot plot(x) Change or variation (sometimes
- ver time)
Scatter plot scatter(x,y) Relation between x and y Histogram (aka Bar Chart) hist(x) Distribution over categories of x
Slide 5 Fall 201 2015
Simple statistical measures: mean (average) range (min-max) median (middle value) More complex statistical measures: regressions …..
CT@VT Line plot visualizations
Slide 6 Fall 201 2015
import earthquakes # get all the reports of earthquakes of the current day quakes = earthquakes.get_report('day', 'all') quake_list = quakes[“earthquakes”] #create an empty list significance_list = [] for quake in quakes_list: # add the significance of the next earthquake to the list significance_list.append(quake["significance"]) plt.plot(significance_list) plt.show()
Click to save
Question: What is the variation in earthquake intensity?
CT@VT Scatter plot visualization
Slide 7 Fall 201 2015
Question: What is the relationship between the depth and magnitude of earthquakes?
import earthquakes import matplotlib.pyplot as plt … depths = [ … ] magnitudes = […] … plt.scatter(depths, magnitudes) plt.show()
CT@VT Histogram visualization
Slide 8 Fall 201 2015
Question: What is the distribution of the magnitudes of earthquakes? import earthquakes import matplotlib.pyplot as plt … magnitudes = [ … ] … plt.hist(magnitudes) plt.show()
CT@VT Next Steps
Work as usual to complete the classwork
for today
Homework gives you more practice with
these ideas
Slide 9 Fall 201 2015