Beginners Python Cheat Sheet PCC Matplotlib BW
Beginners Python Cheat Sheet PCC Matplotlib BW
Beginners Python Cheat Sheet PCC Matplotlib BW
x_values = list(range(1000))
A colormap varies the point colors from one shade to another, based
on a certain value for each point. The value used to determine Python Crash Course
squares = [x**2 for x in x_values] the color of each point is passed to the c argument, and the cmap A Hands-on, Project-Based
argument specifies which colormap to use. Introduction to Programming
fig, ax = plt.subplots() ax.scatter(x_values, squares, c=squares,
ax.scatter(x_values, squares, s=10) ehmatthes.github.io/pcc_3e
cmap=plt.cm.Blues, s=10)
plt.show()
Multiple plots Working with dates and times (cont.) Multiple plots in one figure
You can make as many plots as you want on one figure. Datetime formatting arguments You can include as many individual graphs in one figure as
When you make multiple plots, you can emphasize The strptime() function generates a datetime object from a you want.
relationships in the data. For example you can fill the space string, and the strftime() method generates a formatted string
Sharing an x-axis
between two sets of data. from a datetime object. The following codes let you work with dates
The following code plots a set of squares and a set of cubes on
exactly as you need to.
Plotting two sets of data two separate graphs that share a common x-axis. The plt.
Here we use ax.scatter() twice to plot square numbers and %A Weekday name, such as Monday subplots() function returns a figure object and a tuple of axes.
cubes on the same figure. %B Month name, such as January Each set of axes corresponds to a separate plot in the figure.
%m Month, as a number (01 to 12) The first two arguments control the number of rows and columns
import matplotlib.pyplot as plt %d Day of the month, as a number (01 to 31) generated in the figure.
%Y Four-digit year, such as 2021 import matplotlib.pyplot as plt
x_values = list(range(11)) %y Two-digit year, such as 21
squares = [x**2 for x in x_values] %H Hour, in 24-hour format (00 to 23)
cubes = [x**3 for x in x_values] x_values = list(range(11))
%I Hour, in 12-hour format (01 to 12) squares = [x**2 for x in x_values]
%p AM or PM cubes = [x**3 for x in x_values]
plt.style.use('seaborn-v0_8') %M Minutes (00 to 59)
fig, ax = plt.subplots() %S Seconds (00 to 61) fig, axs = plt.subplots(2, 1, sharex=True)
ax.scatter(x_values, squares, c='blue', s=10) Converting a string to a datetime object
ax.scatter(x_values, cubes, c='red', s=10) axs[0].scatter(x_values, squares)
new_years = dt.strptime('1/1/2023', '%m/%d/%Y') axs[0].set_title('Squares')
plt.show() Converting a datetime object to a string axs[1].scatter(x_values, cubes, c='red')
Filling the space between data sets ny_string = new_years.strftime('%B %d, %Y') axs[1].set_title('Cubes')
The fill_between() method fills the space between two data sets. print(ny_string)
It takes a series of x-values and two series of y-values. It also takes plt.show()
a facecolor to use for the fill, and an optional alpha argument that Plotting high temperatures
controls the color’s transparency. The following code creates a list of dates and a corresponding list of Sharing a y-axis
high temperatures. It then plots the high temperatures, with the date To share a y-axis, use the sharey=True argument.
ax.fill_between(x_values, cubes, squares, labels displayed in a specific format.
facecolor='blue', alpha=0.25) import matplotlib.pyplot as plt
from datetime import datetime as dt
x_values = list(range(11))
Working with dates and times import matplotlib.pyplot as plt squares = [x**2 for x in x_values]
Many interesting data sets have a date or time as the x from matplotlib import dates as mdates cubes = [x**3 for x in x_values]
value. Python’s datetime module helps you work with this
kind of data. dates = [ plt.style.use('seaborn-v0_8')
dt(2023, 6, 21), dt(2023, 6, 22), fig, axs = plt.subplots(1, 2, sharey=True)
Generating the current date dt(2023, 6, 23), dt(2023, 6, 24),
The datetime.now() function returns a datetime object ] axs[0].scatter(x_values, squares)
representing the current date and time.
axs[0].set_title('Squares')
from datetime import datetime as dt highs = [56, 57, 57, 64]
axs[1].scatter(x_values, cubes, c='red')
today = dt.now() plt.style.use('seaborn-v0_8') axs[1].set_title('Cubes')
date_string = today.strftime('%m/%d/%Y') fig, ax = plt.subplots()
print(date_string) ax.plot(dates, highs, c='red') plt.show()
Generating a specific date ax.set_title("Daily High Temps", fontsize=24)
You can also generate a datetime object for any date and time you ax.set_ylabel("Temp (F)", fontsize=16)
want. The positional order of arguments is year, month, and day. x_axis = ax.get_xaxis()
The hour, minute, second, and microsecond arguments are optional. x_axis.set_major_formatter(
from datetime import datetime as dt mdates.DateFormatter('%B %d %Y')
)
new_years = dt(2023, 1, 1) fig.autofmt_xdate()
fall_equinox = dt(year=2023, month=9, day=22) Weekly posts about all things Python
plt.show()
mostlypython.substack.com