matplotlib.
pyplot Cheat Sheet
by gabriellerab via cheatography.com/61175/cs/16431/
Importing the library Key functions Multiple plots
import matplotlib.pyplot as plt plt.clf() Clear figure plt.plot(x_data1, y_data1)
plt.savefig("filena Save figure (call before plt.plot(x_data2, y_data2)
Plots and key arguments me") plt.show()) plt.plot(x_data3, y_data3)
Line plt.plot() (x_data, y_data) plt.show() Show figure plt.show()
graph You can put multiple plots in one figure by
Axis functions
Scatter plt.scatt (x_data, y_data) defining each one before plt.show() or
plot er() plt.xlim(xmin, Set the limits for the x axis plt.savefig()
Bar chart plt.bar() (x_locs, bar_heights, xmax)
width = int) Using colormaps
plt.ylim(ymin, Set the limits for the y axis
Histogram plt.hist() (data, bins = int) ymax) # Choose a colormap and assign to
Pie chart plt.pie() (data, labels = list) plt.xscale("scale Set scale for the x axis a variable
type") (ex. "log") cm = plt.cm.get_cmap("RdYlBu")
Optional arguments plt.yscale("scale Set scale for the y axis # Set the color map in a plot
type") (ex. "log") plt.scatter(x_data, y_data,
color Change plot color
="color" plt.twinx() Add a second y axis cmap=cm)
marker = Change marker for line or scatter plt.axis("off") Do not show the axes
See all colormaps here:
"symbol" plot (".", "x", "|", "o") plt.gca().invert_ Invert the x axis https://matplotlib.org/users/colormaps.html
markersize Change marker size xaxis()
= int plt.gca().invert_ Invert the y axis
linewidth = Change line width for line graph yaxis()
int
Labeling functions
cmap = Color plot according to a
colormap colormap plt.title("title") Add a title
plt.xlabel("x axis label") Add a label to the x
axis
plt.ylabel("y axis label") Add a label to the y
axis
plt.legend(loc = int) Add a legend
plt.xticks(range(min, Modify the x axis
max, interval) tick marks
By gabriellerab Published 22nd July, 2018. Sponsored by CrosswordCheats.com
cheatography.com/gabriellerab/ Last updated 22nd July, 2018. Learn to solve cryptic crosswords!
Page 1 of 1. http://crosswordcheats.com