\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{sajid\_ali\_1} \pdfinfo{ /Title (matplotlib.pdf) /Creator (Cheatography) /Author (sajid\_ali\_1) /Subject (Matplotlib Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{26A318} \definecolor{LightBackground}{HTML}{F1F9F0} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Matplotlib Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{sajid\_ali\_1} via \textcolor{DarkBackground}{\uline{cheatography.com/197163/cs/41525/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}sajid\_ali\_1 \\ \uline{cheatography.com/sajid-ali-1} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 3rd December, 2023.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Prepare the Data}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{ 1D Data }}} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{}\textgreater{}\textgreater{} `import numpy as np ` \newline \textgreater{}\textgreater{}\textgreater{} `x = np.linspace(8, 10, 100)` \newline \textgreater{}\textgreater{}\textgreater{} `y = np.cos(x) ` \newline \textgreater{}\textgreater{}\textgreater{} `z = np.sin(x)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Prepare the 2D Data}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{2D Data or Images}}} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{}\textgreater{}\textgreater{} `data = 2 {\emph{ np.random.random ((10,10))` \newline \textgreater{}\textgreater{}\textgreater{} `data2 = 3}} np.random.random((10,10)` \newline \textgreater{}\textgreater{}\textgreater{} `Y, X = np.mgrid{[}-3:3:180j, -3:3:180j{]}` \newline \textgreater{}\textgreater{}\textgreater{} `U = -1 - X {\bf{ 2 + Y ` \newline \textgreater{}\textgreater{}\textgreater{} `V = 1 + X - Y }} 2` \newline \textgreater{}\textgreater{}\textgreater{} `from matplotlib.cbook import get\_sample\_data ` \newline \textgreater{}\textgreater{}\textgreater{} `img = \seqsplit{np.load(get\_sample\_data('axes\_grid/biveriate\_normal.npy))`}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Create Plot}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{}\textgreater{}\textgreater{} `import matplotlib.pyplot as plt`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Figure}}} \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{}\textgreater{}\textgreater{} `fig = plt.figure() ` \newline \textgreater{}\textgreater{}\textgreater{} `fig2 = \seqsplit{plt.figure(figsize=plt.figaspect(2},0)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Create Plot}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Axes}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{All plotting is done with respect to an Axes. In most cases, a subplot will fit your needs.} \tn % Row Count 3 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{}\textgreater{}\textgreater{} `fig.add\_axes()` \newline \textgreater{}\textgreater{}\textgreater{} `ax1 = fig.add\_subplot(221)` \newline \textgreater{}\textgreater{}\textgreater{} `ax3 =fig.add\_subplot(212)` \newline \textgreater{}\textgreater{}\textgreater{} `fig3, axes =plt.subplots(mrows=2,mcols=2)` \newline \textgreater{}\textgreater{}\textgreater{} `fig4, axes2 = plt.subplots(ncols=3)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Save Plot}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{}\textgreater{}\textgreater{} plt.savefig('foa.png') \newline \textgreater{}\textgreater{}\textgreater{} plt.savefig ('foag.png, transparent=True)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Show Plot}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{}\textgreater{}\textgreater{} plt.show()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Plotting Routines}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{1D Data}}} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{}\textgreater{}\textgreater{} `fig, ax = plt.subplots()` \newline \textgreater{}\textgreater{}\textgreater{} `lines = ax.plot(x,y)` \newline \textgreater{}\textgreater{}\textgreater{} `ax.scatter(x,y)` \newline \textgreater{}\textgreater{}\textgreater{} `axes{[}0,0{]}.bar({[}1,2,3{]},{[}3,4,5{]})` \newline \textgreater{}\textgreater{}\textgreater{} `axes{[}1,0{]}.barh({[}0.5,1,2.5{]},{[}0,1,2{]})` \newline \textgreater{}\textgreater{}\textgreater{} `axes{[}1,1{]}.axhline(0.45)` \newline \textgreater{}\textgreater{}\textgreater{} `axes{[}0,1{]}.axvline(0.65)` \newline \textgreater{}\textgreater{}\textgreater{} `ax.fill(x,y,color='blue')` \newline \textgreater{}\textgreater{}\textgreater{} `ax.fill\_between(x,y,color='yellow')`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Plotting Routines}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{2D Data}}} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{}\textgreater{}\textgreater{} `fig, ax = plt.subplots()` \newline \textgreater{}\textgreater{}\textgreater{} `im =ax. inshow(img, ` \newline `cmap='girst\_earth', ` \newline `interpolation='nearest', ` \newline `vmin=-2, ` \newline `vmax=2)` \newline \textgreater{}\textgreater{}\textgreater{} `axes2{[}0{]}.pcolor(data2)` \newline \textgreater{}\textgreater{}\textgreater{} `axes2{[}0{]}.pcolormesh(data)` \newline \textgreater{}\textgreater{}\textgreater{} `CS = plt.contour(Y,X,U)` \newline \textgreater{}\textgreater{}\textgreater{} `axes2{[}2{]}.contourf(data1)` \newline \textgreater{}\textgreater{}\textgreater{} `axes2{[}2{]}=ax.clabel(CS)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Vector Fields}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{}\textgreater{}\textgreater{} `axes{[}0,1{]}.arrow(0,0,0.5,0.5)` \newline \textgreater{}\textgreater{}\textgreater{} `axes{[}1,1{]}.quiver(y,z)` \newline \textgreater{}\textgreater{}\textgreater{} `axes{[}0,1{]}.streamplot(X,Y,U,V)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Data Distributions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{}\textgreater{}\textgreater{} `ax1.hist(y)` \newline \textgreater{}\textgreater{}\textgreater{} `ax3.boxplot(y)` \newline \textgreater{}\textgreater{}\textgreater{} `ax3.violinplot(z)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Plot Anatomy \& Workflow}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Plot Anatomy} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Workflow} \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The basic steps to creating plots with matplotlib are: \newline 1. Prepare Data 2. Create Plot 3. Plot 4. Customized Plot 5. Save Plot 6. Show Plot \newline \newline `\textgreater{}\textgreater{}\textgreater{} import matplotlib.pyplot as plt` \newline `\textgreater{}\textgreater{}\textgreater{} x = {[}1,2,3,4{]}` \newline `\textgreater{}\textgreater{}\textgreater{} y = {[}10,20,25,30{]}` \newline `\textgreater{}\textgreater{}\textgreater{} fig = plt.figure() ` \newline `\textgreater{}\textgreater{}\textgreater{} ax.plot(x, y, color ='lightblue', linewidth=3)` \newline `\textgreater{}\textgreater{}\textgreater{} ax.scatter({[}2,4,6{]}),{[}5,15,25{]}, ` \newline `color='darkgreen',` \newline `marker='\textasciicircum{}')` \newline `\textgreater{}\textgreater{}\textgreater{} ax.set\_xlin(1, 6.5)` \newline `\textgreater{}\textgreater{}\textgreater{} plt.savefig('foa.png')` \newline `\textgreater{}\textgreater{}\textgreater{} plt.show()`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Close and Clear}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`\textgreater{}\textgreater{}\textgreater{} plt.cla()` \newline `\textgreater{}\textgreater{}\textgreater{} plt.clf()` \newline `\textgreater{}\textgreater{}\textgreater{} plt.close()`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Plotting Customize Plot}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Colors, Color Bars \& Color Maps}}} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`\textgreater{}\textgreater{}\textgreater{} plt.plot(x,x,x,x{\bf{2, x, x}}3)` \newline `\textgreater{}\textgreater{}\textgreater{} ax.plot(x, y, alpha=0.4)` \newline `\textgreater{}\textgreater{}\textgreater{} ax.plot(x, y, c='k')` \newline `\textgreater{}\textgreater{}\textgreater{} fig.colorbar(in, \seqsplit{orientation='horizontal')`} \newline `\textgreater{}\textgreater{}\textgreater{} in = ax.inshow(img, cmap='seismic')`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Markers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`\textgreater{}\textgreater{}\textgreater{} fig, ax=plt.subplots()` \newline `\textgreater{}\textgreater{}\textgreater{} ax.scatter(x,y, marker='',"` \newline `\textgreater{}\textgreater{}\textgreater{} ax.plot(x,y, marker="0")`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Linestyles}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`plt.plot(x,y,linewidth=4.0)` \newline `plt.plot(x,y,ls='solid')` \newline `plt.plot(x,y,ls='-{}-')` \newline `plt.plot(x,y,,'-{}-',x{\bf{2,y}}2,'-.')` \newline `plt.setp(lines,color='r', linewidth=4.0)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Text \& Annotations}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`\textgreater{}\textgreater{}\textgreater{} ax.text(1,-2.1, 'Example Graph', style='italic')` \newline `\textgreater{}\textgreater{}\textgreater{}ax.annotate("Sine", ` \newline `xy =(0,0),xycoords='data',` \newline ` xytext=(10.5,0),textcoords='data',` \newline ` arrowprops=dict(arrowstyle="-{}-{}-\textgreater{}", ` \newline `connectionstyle='arc3'),)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Mathtext}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`\textgreater{}\textgreater{}\textgreater{}plt.title(r'\$sigma\_i=15\$', fontsize=20)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Limits, Legends and Layouts}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Limits \& Autoscaling }} \newline \textgreater{}\textgreater{}\textgreater{} ax.margins(x=0.0, y=0.1) \newline \textgreater{}\textgreater{}\textgreater{} ax.axis('equal') \newline \textgreater{}\textgreater{}\textgreater{} ax.set(xlin={[}0,10.5{]}, \newline ylin={[}-1.5,1.5{]} \newline \textgreater{}\textgreater{}\textgreater{} ax.set\_xlin(0,10.5) \newline {\bf{Legends}} \newline \textgreater{}\textgreater{}\textgreater{} ax.set(title='An Example Axes' \newline ylabel="Y-axis', xlabel='X-Axis") \newline \textgreater{}\textgreater{}\textgreater{}ax.legend(loc="best") \newline {\bf{Ticks}} \newline \textgreater{}\textgreater{}\textgreater{}ax.xaxis.set(ticks=range(1,5), \newline ticklabels={[}3,100,-12,"foa"{]}) \newline \textgreater{}\textgreater{}\textgreater{} ax.tick\_parama(axis='y', \newline direction='inout', length=10) \newline {\bf{Subplot Spacing }} \newline \textgreater{}\textgreater{}\textgreater{} \seqsplit{fig3.subplots\_adjust(wspace=0.5}, \newline hspace =0.3, left=0.125, right=0.9,button=0.1) \newline {\bf{Axis Spines}} \newline \textgreater{}\textgreater{}\textgreater{} ax1.spines{[}'top'{]}.set\_visible(False) \newline \textgreater{}\textgreater{}\textgreater{} ax1.spines{[}'button'{]}.set\_position(('outward',10))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}