Matplotlib Flashcards
What is Matplotlib
Low level graph plotting library. For visualization.
Via python, show the matplotlib version
import matplotlib
print(matplotlib.__version__)
Remember, __version__ is a dunder method.
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.
import matplotlib.pyplot as plt
Draw a line in a diagram from position (0,0) ro (6,250)
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.
in terms of plt.plot, what goes first, x or y axis?
Where is the x axis located?
Where is the y axis located?
x goes first:
plt.plot(x, y)
x is horizontal
y is vertical
Draw a line diagram
x axis - start from 1 and go up to 8
y axis - start from 3 and go up to 10
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()
Via diagram, mark two points:
1, 3
8, 10
remember that the x axis goes first
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()
Now create a line diagram that goes between the following points:
1,3
2,8
6,1
8,10
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()
What happens if we only have the following ypoints and no x points defined:
ypoints = np.array([3, 8, 1, 10, 5, 7])
x will start from 1 and go to the higher depending on y
Create a line diagram, but add a point to each peak and valley of the line, next add a star
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 = ‘*’
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?
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’
Change the line color to red
The width of the line to 20.5
plt.plot(ypoints, color = ‘r’, lw = ‘20.5’)
Create two lines in the same graph
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()
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
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()
Create two diagrams and give them a title
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()