2 Introduction to Python (II)
No description yet. This curated deck is ready for daily study.
Card preview (24)
1 What is Matplotlib?
A plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI
2 Complete code: import matplot… as …
import matplotlib.pyplot as plt
3 Make a line plot (year x-axis, pop y-axis) year=[‘1975’,’1976’,’1977’] pop=[2340,2405,2890]
import matplotlib.pyplot as plt plt. plot(year,pop) plt. show()
4 How to display a matplotlib plot?
plt.show()
5 Print the last item of the list year: year=[‘1975’,’1976’,’1977’]
print(year[-1]) print(year[2])
6 What is a scatter plot?
A type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables for a set of data
7 Complete code (scatter plot): x = [1,3,5] y= [2,6,7] ’'’import mat…. … plt.show()’’’
import matplotlib.pyplot as plt plt. scatter(x,y) plt. show()
8 Change the line plot below to a scatter plot year=[‘1975’,’1976’,’1977’] pop=[2340,2405,2890] import matplotlib.pyplot as plt plt. plot(year,pop) plt. show()
plt. scatter(year,pop) plt. show()
9 Put the x-axis on a logarithmic scale day=[‘1’,’2’,’3’] virus=[18,55,320] import matplotlib.pyplot as plt plt. scatter(day,virus) plt. show()
plt. scatter(day,virus) plt. xscale(‘log’) plt. show()
10 What is a correlation coefficient?
A value that indicates the strength of the relationship between variables. The coefficient can take any values from -1 to 1.
11 What is a histogram?
An approximate representation of the distribution of numerical or categorical data
12 Create histogram years = [1975,1976,1978,1975]
import matplotlib.pyplot as plt plt. hist(years) plt. show()
13 Create histogram with 5 bins using data (list) data = [random.randint(1, 5) for _ in range(100)]
plt.hist(data,bins=5)
14 What is the use of plt.clf() ?
Cleans a plot up again so you can start afresh
15 You want to visually assess if the grades on your exam follow a particular distribution. Which plot do you use?
Histogram
16 You want to visually assess if longer answers on exam questions lead to higher grades. Which plot do you use?
Scatter plot
17 Add labels year =list(range(1975,2000)) scores = list(range(1,26)) plt.scatter(year,scores) …
plt. xlabel(‘year’) plt. ylabel(‘scores’) plt. show()
18 Add ‘scores’ as a title data = [int(random.randint(1, 5)) for _ in range(100)] plt.hist(data,bins=5) … plt.plot()
plt.title(‘years’)
19 Add log scale year =list(range(1975,2000)) scores= [2**n for n in range(25)] plt.scatter(year,scores) …
plt. yscale(‘log’) plt. show()
20 What are ticks in matplotlib?
Ticks are the values used to show specific points on the coordinate axis. It can be a number or a string.
21 What is a legend in matplotlib?
The legend of a graph reflects the data displayed in the graph’s Y-axis
22 Change the ticks in the x-axis to strings x=[1, 3, 5] y=[1, 5, 9] import matplotlib.pyplot as plt plt.scatter(x,y)
plt. xticks(x, [“one”,”three”,”five”]) plt. show()
23 Write a scatter plot with gdp as independent variable and population size as the size argument gdp=[100, 200, 300] life_exp=[50, 70, 82] pop_size=[30,20,40]
import matplotlib.pyplot as plt plt. scatter(gdp, life_exp, s =pop_size) plt. show()
24 What is a dependent variable?
A variable (often denoted by y ) whose value depends on that of another.