-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotting_examples.py
More file actions
48 lines (32 loc) · 1.19 KB
/
plotting_examples.py
File metadata and controls
48 lines (32 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# This code gives some examples for plotting of data
# Getting data using .loc[] and .iloc[]
pandas.DataFrame.iloc[i,j] # where i is the row and j the column
df.iloc[0:3, 1]
# Other option with .loc[]
pandas.DataFrame.loc["name in index", "name in columns"]
df.loc["Aruba", "1960"]
df.loc["Aruba", ["1960", "1980"]]
df.loc["Aruba", "1960": "1960"]
# Subsetting using a boolean
rows_of_interest = d2["Inidcator"]== "urban population (% of total)"
# Using the variable "rows_of_interest" to subset d2
d_subset = d2.loc[rows_of_interest]
# We have used a series of plotting commands from the matplotlib.pyplot library. The start for plotting was always to create our figure and axes objects with the .subplots() command, e.g.:
fig, ax = plt.subplots(nrows=1, ncols=1)
# ax.plot()
# ax.pie()
# ax.scatter()
# ax.bar()
# Below, a plain plot is plotted
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, ncols=1)
x = d_s1x # the years
y1 = d_s1y1 # United States
y2 = d_s1y2 # China
ax.plot(x,y1, label = country1)
ax.plot(x,y2, label = country2)
# making the plot pretty
ax.set_title(descriptor1+' for '+ country1 + ' and ' + country2)
ax.set_xlabel('year')
ax.set_ylabel(descriptor1)
ax.legend()