DC Code Flashcards
What way do we use matplotlib?
Using the main object-oriented interface provided through the pyplot submodule.
How do we import the pyplot submodule?
import matplotlib.pyplot as plt
What does the plt.subplots() command do?
Creates two different objects - an axes and figure object.
What is the figure object?
A container which holds everything you see on the page.
What is the axes object?
The part of the page which holds the data.
What is the code to create an empty axes?
import matplotlib as plt
fig, ax = plt.subplots()
plt.show()
How do we add data to the axes?
ax.plot(data[“COL-1”], data[“COL-2”])
How do you add data to a figure?
Using the Axes object
What ways can you customise plots?
- Linestyle
- Point style (markers)
- Colours
- Add axes labels
- Add title label
Why are markers useful?
If a plot appears continuous, markers show us where the data exists and which parts are just lines connecting the data points.
How do you add a marker?
ax.plot(marker = “o”)
How do you edit the lifestyle?
ax.plot(linestyle=”–”)
How do you remove a line from the plot?
ax.plot(linestyle=”None”)
How do you edit the colour of the plotted data?
ax.plot(color=”r”)
How do you add axes labels?
ax.set_xlabel(“Text”)
ax.set_ylabel(“Text”)
What convention is used for capitalising the title?
Write it as you would a sentence - only first words and proper nouns are capitalised.
How do you add a plot title?
ax.set_title()
What are small multiples and what do they achieve?
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.
What are small multiples called in matplotlib?
Subplots
What does calling subplots() with no inputs achieve?
Creates one subplot.
What does passing inputs to subplots() achieve?
subplots(X, Y)
Small multiples are arranged on the page as a grid with rows and columns - X rows and Y columns.
When creating small multiples, what can be said of the variable ax?
Ax is no longer only one aces object, it is now an array of axes object with shape X by Y.
How can you investigate the number of small multiples?
ax.shape
Shows the shape of the ax array.
When we have small multiples, how do we add data using ax.plt()?
We now need to index the axes object and call the plot method on an element of the array.
eg ax[0,0].plt()