1. Installing Matplotlib
python3 -m pip install --user matplotlib
2. Plotting a Simple Line Graph
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
fig, ax = plt.subplots()
ax.plot(squares)
plt.show()
3. Changing the Label Type and Line Thickness
mpl_squares.py
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
fig, ax = plt.subplots()
# the linewidth parameter controls the thickness of the line that plot() generates.
ax.plot(squares, linewidth=3)
# Set chart title and label axes.
# the set_title() method sets an overall title for the chart.
ax.set_title("Square Numbers", fontsize=24)
# the set_xlabel() and set_ylabel() methods allow you to set a title for each of the axes
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
# Set size of tick labels
ax.tick_params(labelsize=14)
plt.show()
4. Correcting the Plot
import matplotlib.pyplot as plt
input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
fig, ax = plt.subplots()
ax.plot(input_values, squares, linewidth=3)
# Set chart title and label axes.
ax.set_title("Square Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
# Set size of tick labels
ax.tick_params(labelsize=14)
plt.show()
5. Using Built-in Styles
to see the full list of available styles, run the fillowing lines in a terminal session:
To use any of these styles, add one line of code before calling subplots():
import matplotlib.pyplot as plt
input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
plt.style.use('seaborn-v0_8')
fig, ax = plt.subplots()
ax.plot(input_values, squares, linewidth=3)
# Set chart title and label axes.
ax.set_title("Square Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
# Set size of tick labels
ax.tick_params(labelsize=14)
plt.show()
6.Plotting and Styling Individual Points with scatter()
import matplotlib.pyplot as plt
#input_values = [1, 2, 3, 4, 5]
#squares = [1, 4, 9, 16, 25]
plt.style.use('seaborn-v0_8')
fig, ax = plt.subplots()
ax.scatter(2, 4, s=200)
#ax.plot(input_values, squares, linewidth=3)
# Set chart title and label axes.
ax.set_title("Square Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
# Set size of tick labels
ax.tick_params(labelsize=14)
plt.show()
7.Plotting a Series of Points with Scatter()
import matplotlib.pyplot as plt
x_values = [1, 2, 3, 4, 5]
y_squares = [1, 4, 9, 16, 25]
plt.style.use('seaborn-v0_8')
fig, ax = plt.subplots()
ax.scatter(x_values, y_squares, s=100)
#ax.plot(input_values, squares, linewidth=3)
# Set chart title and label axes.
ax.set_title("Square Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
# Set size of tick labels
ax.tick_params(labelsize=14)
plt.show()
8.Calculating Data Automatically
import matplotlib.pyplot as plt
x_values = range(1, 1001)
y_squares = [x**2 for x in x_values]
plt.style.use('seaborn-v0_8')
fig, ax = plt.subplots()
ax.scatter(x_values, y_squares, s=10)
#ax.plot(input_values, squares, linewidth=3)
# Set chart title and label axes.
ax.set_title("Square Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
# Set size of tick labels
ax.tick_params(labelsize=14)
# Set the range for each axis.
ax.axis([0, 1100, 0, 1_100_000])
plt.show()
9.Customizing Tick Labels
import matplotlib.pyplot as plt
x_values = range(1, 1001)
y_squares = [x**2 for x in x_values]
plt.style.use('seaborn-v0_8')
fig, ax = plt.subplots()
ax.scatter(x_values, y_squares, s=10)
#ax.plot(input_values, squares, linewidth=3)
# Set chart title and label axes.
ax.set_title("Square Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
# Set size of tick labels
ax.tick_params(labelsize=14)
# Set the range for each axis.
ax.axis([0, 1100, 0, 1_100_000])
ax.ticklabel_format(style='plain')
plt.show()
10.Defining Custom Colors
import matplotlib.pyplot as plt
x_values = range(1, 1001)
y_values = [x**2 for x in x_values]
plt.style.use('seaborn-v0_8')
fig, ax = plt.subplots()
ax.scatter(x_values, y_values, s=10)
#ax.plot(input_values, squares, linewidth=3)
# Set chart title and label axes.
ax.set_title("Square Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
# Set size of tick labels
ax.tick_params(labelsize=14)
# Set the range for each axis.
ax.axis([0, 1100, 0, 1_100_000])
ax.ticklabel_format(style='plain')
# defining custom colors
ax.scatter(x_values, y_values, color=(0, 0.8, 0), s=10)
plt.show()
11. Using a Colormap
12.Saving Your Plots Automatically
import matplotlib.pyplot as plt
x_values = range(1, 1001)
y_values = [x**2 for x in x_values]
plt.style.use('seaborn-v0_8')
fig, ax = plt.subplots()
ax.scatter(x_values, y_values,cmap=plt.cm.Blues, s=10)
#ax.plot(input_values, squares, linewidth=3)
# Set chart title and label axes.
ax.set_title("Square Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
# Set size of tick labels
ax.tick_params(labelsize=14)
# Set the range for each axis.
ax.axis([0, 1100, 0, 1_100_000])
ax.ticklabel_format(style='plain')
# defining custom colors
ax.scatter(x_values, y_values, color=(0, 0.8, 0), s=10)
plt.show()
plt.savefig('square_plot.png', bbox_inches='tight')