Python - Matplotlib Cheat Sheet
by Pitbull (aggialavura) via cheatography.com/83764/cs/19841/
TO START SIZE, SAVE, LEGEND
conda install matplotlib plt.tight_layout() avoid overlap
import matplotlib.pyplot as plt plt.fig(figsize=(x,x)) set figuresize
# to print graphs in notebooks
plt.fig(figsize=(x,x), dpi=x) set dpi
%matplotlib inline
fig.savefig("name.png") save figure
fig.savefig("name", dpi=200) ..and set dpi
BASICS
ax.plot(x, y, label="str") set legend
FUNCTIONAL METHOD
ax.legend() show legend
plt.plot(x,y) simple line plot
ax.legend(loc=0) best
-plt.xlabel('str') set x label
ax.legend(loc=1) upper right
-plt.ylabel('str') set y label
ax.legend(loc=2) upper left
-plt.title('str') set title ax.legend(loc=3) lower left
plt.show() show plot ax.legend(loc=4) lower right
plt.subplot(r,c,1)* create multiplots subplot() command requires to specify the number of row and
plt.plot(x, y) column we want to print the plots, and the third parameter specify
plt.subplot(r,c,2) what of the graph we are going to handle.
plt.plot(y, x, 'g*-') axes: ([left, bottom, width, height])
OBJECT ORIENTED METHOD (more control) fig, axes allow you to auto-manage axis, you don't have to create
them. Axes, now, will be an array of axis.We could use for loop to
fig = plt.figure() create canvas
populate labels on axis.
ax = fig.add_axes([0,0,1,1]) create axes*
ax.plot(x, y, 'b') create plot
COLORS, LINEWIDTHS, LINETYPES
ax.set_xlabel(''str) set x label
ax.set_ylabel('str') set y label fig = plt.figure() ----
ax.set_title(''str) set title ax = fig.add_axes([0,0,1,1]) ----
ax.plot(x,y ----
** add more axis to have more figures
color='#xxxxxx', set color
fig, ax = plt.subplots(r,c)* subplots
lw=x, set linewidth
axes[0].plot(x,y) create pl ax1
alpha=x, set alpha
axes[1].plot(x,y) create pl ax2
ls='', set linestyle
axes[0].set_title('str') set plot 1 title
marker='', set markertype
axes[1].set_title(''str) set plot 2 title
markersize=x, set marker size
subplot() command requires to specify the number of row and markerfacecolor='', set mark color
column we want to print the plots, and the third parameter specify markeredgecolor='', set external col
what of the graph we are going to handle. markeredgewidth=x) set marker wdt
axes: ([left, bottom, width, height])
ax.set_xlim([0,1]) set x axes limit
fig, axes allow you to auto-manage axis, you don't have to create
ax.set_ylim([0,1]) set y axes limit
them. Axes, now, will be an array of axis.We could use for loop to
populate labels on axis. ax.plot(x, y, 'r--') MATLAB style
By Pitbull (aggialavura) Not published yet. Sponsored by Readable.com
Last updated 14th June, 2019. Measure your website readability!
Page 1 of 1. https://readable.com
cheatography.com/aggialavura/