DC Code Flashcards

1
Q

What way do we use matplotlib?

A

Using the main object-oriented interface provided through the pyplot submodule.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do we import the pyplot submodule?

A

import matplotlib.pyplot as plt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the plt.subplots() command do?

A

Creates two different objects - an axes and figure object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the figure object?

A

A container which holds everything you see on the page.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the axes object?

A

The part of the page which holds the data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the code to create an empty axes?

A

import matplotlib as plt
fig, ax = plt.subplots()
plt.show()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do we add data to the axes?

A

ax.plot(data[“COL-1”], data[“COL-2”])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you add data to a figure?

A

Using the Axes object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What ways can you customise plots?

A
  • Linestyle
  • Point style (markers)
  • Colours
  • Add axes labels
  • Add title label
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why are markers useful?

A

If a plot appears continuous, markers show us where the data exists and which parts are just lines connecting the data points.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you add a marker?

A

ax.plot(marker = “o”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you edit the lifestyle?

A

ax.plot(linestyle=”–”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you remove a line from the plot?

A

ax.plot(linestyle=”None”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you edit the colour of the plotted data?

A

ax.plot(color=”r”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you add axes labels?

A

ax.set_xlabel(“Text”)
ax.set_ylabel(“Text”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What convention is used for capitalising the title?

A

Write it as you would a sentence - only first words and proper nouns are capitalised.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you add a plot title?

A

ax.set_title()

18
Q

What are small multiples and what do they achieve?

A

Small multiples are used to plot several datasets side-by-side. They show similar data across different conditions.

They are a way to reduce the clutter/mess associated when too much data is plotted on one graph. This helps to view trends better.

19
Q

What are small multiples called in matplotlib?

A

Subplots

20
Q

What does calling subplots() with no inputs achieve?

A

Creates one subplot.

21
Q

What does passing inputs to subplots() achieve?

A

subplots(X, Y)

Small multiples are arranged on the page as a grid with rows and columns - X rows and Y columns.

22
Q

When creating small multiples, what can be said of the variable ax?

A

Ax is no longer only one aces object, it is now an array of axes object with shape X by Y.

23
Q

How can you investigate the number of small multiples?

A

ax.shape

Shows the shape of the ax array.

24
Q

When we have small multiples, how do we add data using ax.plt()?

A

We now need to index the axes object and call the plot method on an element of the array.

eg ax[0,0].plt()

25
Q

If there is only one row or one column of plots, how do we call ax.plot?

A

The resulting array is one dimensional, so you only provide one index to access the elements of the array.

eg ax[0].plt and ax[1].plt

26
Q

When adding x and y labels to small multiples, what should we consider?

A

Eg for 2 x 1, we only need to add one X axes label to the bottom plot.

27
Q

How do we account for different axis ranges of plots?

A

plt.subplots(2, 1, sharey=True)

Want to make sure that the subplots have the same range of y axis values. We initialise the figure and its subplots with the keyword argument sharey=True

This improves comparison across datasets

28
Q

If we import a pandas dataframe which represents a time series, what do we need to do?

A

Tell Pandas to Parse the date column as a date.

import pandas as pd
df = pd.read_csv(“file.csv”, parse_dates = [“date”], index_col = “date”)

Where date is the column name.

29
Q

How do we access the index (now a date) of a dataframe?

A

df.index

30
Q

Once the date is set as the index, how do we plot the time series data?

A

ax.plot(df.index, df[|”col”])

31
Q

How do we plot a particular time period?

A

Slice the DataFrame using two strings to denote the start and end date.

sixties = df[“1960-01-01”:”1969-12-31”]

Use this dataframe in the plot.

Matplotlib will automatically change the axis ticks.

32
Q

If we want to plot two variables with different scales on the same plot, what can we do?

A

Plot on the same subplot using two different y axis plots. Utilising the twins() method to create a twin of the axes. Share the same x axis, but the y axes are separate.

ax2 = ax.twinx()

33
Q

How can we further highlight the different axis?

A

Giving each variable its own colour, and the y axis, labels and ticks are the same colour. Add colour to ax.plot(), ax.set_ylabel() and ax.tick_params()

ax.tick_params() clarified in next point

34
Q

How do we set the colour of the axis ticks?

A

ax.tick_params(“y”, colors = “blue”)

  • NB: s in colorS
  • First argument takes either x or y
35
Q

How can we prevent retyping out the same code to colour the data of a time series?

A

Make a function we can reuse.

def plot_timeseries(axes, x, y, color, xlabel, ylabel):
axes.plot(x, y, color = color)
axes.set_xlabel(xlabel)
axes.set_ylabel(ylabel, color = color)
axes.tick_params(“y”, colors = color)

Then call this function passing in the relevant variables.

36
Q

How do we add an annotation to a visualisation?

A

Using a method of the axes object, annotate.

ax.annotate()

37
Q

What are the minimum inputs for annotate()

A

ax.annotate(“Annotation text”, xy=(pd.Timestamp(“2015-10-06”), 1))

At the very least it takes the annotation text as input (string) and the xy coordinate we want to annotate.

38
Q

If the x position to annotate is a time stamp, how do we define it?

A

Using the Pandas object

pd.Timestamp(“2015-10-06”)

39
Q

How do we position the text in an appropriate place?

A

add argument xytext=(pd.Timestamp(“2008-10-06”), -0.2)

May need to experiment to get a good position

40
Q

How do we connect an arrow between the annotation text and the annotated data?

A

Add the keyword arrowprops

arrowprops={}

Passing in an empty dictionary results in the default arrow.

41
Q

How do we customise the arrow?

A