MatPlotLib Introduction¶
The main interest we have here at the FabLab in the Jupyter Notebook is data science. Our goal is to make it so that you can make powerful data visualizations. We'll be showing some examples of what we can do in jupyter to get you started. From there we'll have an idea of what we might want to do with other tools.
We'll start with how we can import the libraries necessary.
At very minimum we need to import numpy and matplotliib.
import matplotlib.pyplot as plt
import numpy as np
Simple Plot¶
Next we can make a very simple plot. We must first create a figure. "fig" will be our figure. We can then plot the x and y values. Our x values below are 1-5. Your y values are chosen to look like a smile (4,3,3,3,4). Lastly we tell plt to show the figure.
fig, ax = plt.subplots() #Create a figure
ax.plot([1, 2, 3, 4, 5], [4, 3, 3, 3, 4]) #Plot a graph that looks like a smile
plt.show() #Show the figure below
Colorful Plot¶
Let's define a function, create y values from that function and then make a graph.
First we'll define the x values, let's create a list with the numbers 1-360. Next, we'll create our y values, defining them as the output of the sine function. For fun, we'll make the background black and the foreground teal(facecolor). Last we'll show the graph.
fig2, ax2 = plt.subplots(facecolor=(0,.95, .65)) #teal background
ax2.set_facecolor("#ffffa7") #bright yellow graph
ax2.set_title('Sine Graph', color="#350a6f") #purple title
x_values=np.linspace(0, 360, num=200) #All the numbers from 0, to 360, in incriments of 1, using the np library
y_values=np.sin(x_values/360*2*np.pi) # the sine of each value in x_values (the function is in radians, so . . . math)
ax2.plot(x_values, y_values, color="#c117d5") #magenta sine wave graph
plt.show()
Simple Scatter Plot¶
Making a scatter plot is simple, we just need the plt.scatter function. We give it x and y values and it works. But you might not think of x_values and then y_values. You probably think of them in (x, y) pairs. So I'm goign to start by making a helper function to add pairs to the two seperate arrays.
Next, we'll add some simple x and y values to our list and graph it.
x_values = []
y_values = []
def add_values(point):
x_values.append(point[0])
y_values.append(point[1])
add_values((2, 3))
add_values((4, 5))
add_values((8,7))
add_values((3, 9))
plt.scatter(x_values, y_values)
<matplotlib.collections.PathCollection at 0x10a1f9460>
Let's spice this up a little by adding different radii to the scatter plot.
radius_values = (150, 200, 50, 10)
plt.scatter(x_values, y_values, s=radius_values)
<matplotlib.collections.PathCollection at 0x109de8f40>
Let's improve one more time, adding colors. I'm going to have a fall theme and go with colors in an orange pallet. I'll use hex codes for this.
color_values = ["#e3a333", "#f0631c", "#ef3e2d", "#eabc37"]
plt.scatter(x_values, y_values, s=radius_values, c=color_values)
<matplotlib.collections.PathCollection at 0x109f7c9a0>
Let's make a grand scatter plot. This one is from the matplotlib quickstart guide you can reference here.
np.random.seed(19680801) # seed the random number generator.
data = {'a': np.arange(50),
'c': np.random.randint(0, 50, 50),
'd': np.random.randn(50)}
data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100
fig, ax = plt.subplots(figsize=(5, 2.7), layout='constrained')
ax.scatter('a', 'b', c='c', s='d', data=data)
ax.set_xlabel('entry a')
ax.set_ylabel('entry b')
Text(0, 0.5, 'entry b')