Matplotlib Flashcards

1
Q

What is Matplotlib

A

Low level graph plotting library. For visualization.

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

Via python, show the matplotlib version

A

import matplotlib

print(matplotlib.__version__)

Remember, __version__ is a dunder method.

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

We will primarily be using only one submodule for matplotlib. Import it and rename it under the name that is often given to it by developers.

A

import matplotlib.pyplot as plt

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

Draw a line in a diagram from position (0,0) ro (6,250)

A

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([0, 6])
ypoints = np.array([0, 250])

plt.plot(xpoints, ypoints)
plt.show()

numpy is only used to create arrays (elements must be of the same type and are in fixed locations allowing them to be summoned via index numbers.)

Plot() Draws point in the diagram.

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

in terms of plt.plot, what goes first, x or y axis?

Where is the x axis located?

Where is the y axis located?

A

x goes first:
plt.plot(x, y)

x is horizontal
y is vertical

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

Draw a line diagram

x axis - start from 1 and go up to 8

y axis - start from 3 and go up to 10

A

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 8])
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)

plt.show()

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

Via diagram, mark two points:
1, 3
8, 10

remember that the x axis goes first

A

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 8])
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints,)

plt.show()

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

Now create a line diagram that goes between the following points:

1,3
2,8
6,1
8,10

A

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 2, 6, 8])
ypoints = np.array([3, 8, 1, 10])

plt.plot(xpoints, ypoints)
plt.show()

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

What happens if we only have the following ypoints and no x points defined:

ypoints = np.array([3, 8, 1, 10, 5, 7])

A

x will start from 1 and go to the higher depending on y

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

Create a line diagram, but add a point to each peak and valley of the line, next add a star

A

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = ‘o’)
plt.show()

to add a star:
marker = ‘*’

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

Explain format string notation and then use it

Set your marker to be the size of 20
Set the outline of the marker to red
If you weren’t using fsn, how would you change the inner color of your marker?

A

Format String Notation is used to specify your marker on the graph.

It is in the structure of ‘marker|line|color.
Take the below for instance:

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, ‘o:r’, marker = ‘0’, ms = 20, mec = ‘r’)
plt.show()

o means you want a circle as the marker
: means you want the line to be dotted
r means you want the color to be red

you can also have the line be:
- solid
: dotted
– dashed
-. dashed dotted

mfc = ‘r’

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

Change the line color to red
The width of the line to 20.5

A

plt.plot(ypoints, color = ‘r’, lw = ‘20.5’)

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

Create two lines in the same graph

A

import matplotlib.pyplot as plt
import numpy as np

y1 = np.array([3, 8, 1, 10])
y2 = np.array([6, 2, 7, 11])

plt.plot(y1)
plt.plot(y2)

plt.show()

or

x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 10])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 7, 11])

plt.plot(x1, y1, x2, y2)
plt.show()

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

Create a graph where the x axis is called ‘Average pulse’
and the y axis is called ‘Calorie Burnage’
Give the graph a name called ‘Doctor shit’

Put the text in ‘serif’, the color as ‘blue’ and a size of 20

Position the Name of the graph to the left

A

import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

font1 = {‘family’: ‘serif’, ‘color’:’blue’, ‘size’:20}
plt.plot(x, y)

plt.title(“Doctor Shit”, fontdict = font1, loc = ‘left’)
plt.xlabel(“Average Pulse”, fontdict = font1)
plt.ylabel(“Calorie Burnage” fontdict= font1)

plt.show()

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

Create two diagrams and give them a title

A

plot 1:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

Number of the plot is denoted by the last ‘1’
This figure has 1 row and 2 columns
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title(‘SALES’)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

Number of the plot is denoted by the last ‘2’
This plot has 1 row and 2 columns
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title(‘INCOME’)
plt.show()

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

Create two diagrams, give them both a name and then give a main title

A

plot 1:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title(“SALES”)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title(“INCOME”)

plt.suptitle(“MY SHOP”)
plt.show()

17
Q

Let’s compare two sets of data in a single diagram.

You can put whatever you want in them, but they can’t have lines, must only be markers, and have different colors that represent each respective data set

A

day one, the age and speed of 13 cars:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)

x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y)

plt.show()

18
Q

how would you change the colors of your markers on the scatter plot?

What if you wanted to color each dot something different?

A

plt.scatter(x, y, color = ‘hotpink’)

colors = np.array([“red”,”green”,”blue”,”yellow”,”pink”,”black”,”orange”,”purple”,”beige”,”brown”,”gray”,”cyan”,”magenta”])

plt.scatter(x, y, c=colors)

19
Q

How would you change the size of the dots on your scatter plot?

A

plt.scatter(x, y, color = ‘hotpink’, size=20)

20
Q

Create a bar graph

what if we want the bars horizontal?

make the bar height 0.1
Make the bar width 0.1

A

import matplotlib.pyplot as plt
import numpy as np

x = np.array([“A”, “B”, “C”, “D”])
y = np.array([3, 8, 1, 10])

plt.bar(x,y, width = 0.1, height)
plt.show()

for horizontal:
plt.barh(x,y)

HEIGHT MIGHT ONLY BE FOR barh

21
Q
A