2 Introduction to Python (II) Flashcards
Matplotlib, dictionaries, dataframes... https://colab.research.google.com/drive/1fKMFrRbIJQE8Tpa06us0qQPnamBn957z?usp=sharing
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.
25 What is an independent variable?
A variable (often denoted by x ) whose variation does not depend on that of another.
26 Code: Scatter plot with text ‘A’ pointing at the second element
gdp=[100, 200, 300]
life_exp=[50, 70, 82]
import matplotlib.pyplot as plt
plt. scatter(gdp, life_exp)
plt. text(195,65,’A’)
plt. show()
27 Add a grid to a matplot figure
plt.grid(True)
28 Get the position of germany
countries = [‘spain’, ‘france’, ‘germany’, ‘norway’]
countries.index(‘germany’)
29 What is the difference between list and dictionary in Python?
A list is an ordered sequence of objects, whereas dictionaries are unordered sets. But the main difference is that items in dictionaries are accessed via keys and not via their position.
30 Get the keys
europe = {‘spain’:’madrid’, ‘france’:’paris’, ‘germany’:’berlin’, ‘norway’:’oslo’ }
Outcome:
dict_keys([‘spain’, ‘france’, ‘germany’, ‘norway’])
print(europe.keys())
31 Get the capital of norway
europe = {‘spain’:’madrid’, ‘france’:’paris’, ‘germany’:’berlin’, ‘norway’:’oslo’ }
Outcome: oslo
print(europe[‘norway’])
32 Add italy and rome to the dictionary
europe = {‘spain’:’madrid’, ‘france’:’paris’,
‘germany’:’berlin’ }
europe[‘italy’]=’rome’
33 Check whether the dictionary has spain
europe = {‘spain’:’madrid’, ‘france’:’paris’,
‘germany’:’berlin’ }
print(‘spain’ in europe)
34 Outcome of:
europe = {‘spain’:’madrid’, ‘france’:’paris’, ‘germany’:’berlin’, ‘norway’:’oslo’ }
print(‘madrid’ in europe)
FALSE
35 Delete spain
europe = {‘spain’:’madrid’, ‘france’:’paris’,
‘norway’:’oslo’}
del(europe[‘spain’])
36 Update the capital of spain with madrid
europe = {‘spain’:’Barcelona’, ‘france’:’paris’,
‘norway’:’oslo’}
europe[‘spain’]=’madrid’