\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{{[}deleted{]}} \pdfinfo{ /Title (matplotlib.pdf) /Creator (Cheatography) /Author ({[}deleted{]}) /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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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}{{[}deleted{]}} via \textcolor{DarkBackground}{\uline{cheatography.com/121124/cs/22057/}}} \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}{[}deleted{]} \\ \uline{cheatography.com/deleted-121124} \\ \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 21st March, 2020.\\ 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{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Import}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{import}} matplotlib.pyplot {\bf{as}} plt} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Anatomy of a figure}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/brechtdewilde_1584385889_matplotlib.PNG}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Main principle}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/brechtdewilde_1584375593_main.PNG}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{\# Example \newline fig, ax = plt.subplots() \# Create one subplot \newline ax.plot(x1, y1) \# each ax represents one plot on the figure object \newline ax.plot(x2, y2) \# another plot is added to the figure object (with another color) \newline plt.show()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.3984 cm} x{5.5671 cm} x{5.9045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Cutomizing data appearance}} \tn % Row 0 \SetRowColor{LightBackground} ax.plot(x,y, marker = "") & \# indicate each data point on the line & \seqsplit{https://matplotlib}.org/3.2.0/gallery/lines\_bars\_and\_markers/marker\_reference.html\#sphx-glr-gallery-lines-bars-and-markers-marker-reference-py \tn % Row Count 11 (+ 11) % Row 1 \SetRowColor{white} ax.plot(x,y, linestyle = "") & \# change linestyle & \seqsplit{https://matplotlib}.org/3.2.0/gallery/lines\_bars\_and\_markers/linestyles.html\#sphx-glr-gallery-lines-bars-and-markers-linestyles-py \tn % Row Count 21 (+ 10) % Row 2 \SetRowColor{LightBackground} ax.plot(x,y, color = "") & \# change color & \seqsplit{https://matplotlib}.org/3.2.0/gallery/color/named\_colors.html\#sphx-glr-gallery-color-named-colors-py \tn % Row Count 29 (+ 8) % Row 3 \SetRowColor{white} \seqsplit{ax.set\_xlabel("")} & \# name the x axis & \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.3984 cm} x{5.5671 cm} x{5.9045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Cutomizing data appearance (cont)}} \tn % Row 4 \SetRowColor{LightBackground} \seqsplit{ax.set\_ylabel("")} & \# name the y axis & \tn % Row Count 2 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{ax.set\_title("")} & \# give figure a title & \tn % Row Count 4 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{ax.tick\_params("x"/"y"}, colors = "") & \# change the color of the ticks & \tn % Row Count 7 (+ 3) % Row 7 \SetRowColor{white} \seqsplit{ax.set\_xticks(list())} & \# change the tick values of the X axis & Use list comprehension \tn % Row Count 10 (+ 3) % Row 8 \SetRowColor{LightBackground} \seqsplit{ax.set\_yticks(list())} & \# change the tick values of the y axis & Use list comprehension \tn % Row Count 13 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{scatterplot}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{fig, ax = plt.scatter(x, y, s=area, c=colors, alpha=0.5) \newline ax.plot(x, y, linestyle = "None") \newline plt.show()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Subplot}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/brechtdewilde_1584381305_subplot.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{ Create the subplots}} \newline fig, ax = plt.subplots(3, 2) \newline {\bf{First Subplot}} \newline ax{[}0, 0{]}.plot(x, y, color = "green") \newline {\bf{Second Subplot}} \newline ax{[}0, 1{]}.plot(x, y, color = "blue")} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Subplot}} \tn % Row 0 \SetRowColor{LightBackground} fig, ax = plt.subplots(rows, columns, index\_of\_subplot) & fig = container holding one to more axes; ax = individual plot \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} ax.plot({[}x{]}, y, color = "green") & {[}X{]} = X coordinates; Y = Y coordinates \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} plt.show() & Plot the figure. \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} plt.subplots(rows, columns, sharey = True) & All subplots have same range of Y-axis values \tn % Row Count 10 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Time series}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Once there is column with a DateTime datatype then matplotlib will recognize that this is a variable that represents time.% Row Count 3 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.5264 cm} x{11.7436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Time series}} \tn % Row 0 \SetRowColor{LightBackground} Plot time series & ax.plot(time\_variable, variableOfInterest) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} slice time variable & timeVariable{[}"start-date":"end-date"{]} \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Time-series with different variables}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{fix, ax = plt.subplots() \newline ax.plot(time\_variable, variable1, color = "blue") \newline ax.set\_xlabel() \newline ax.set\_ylabel() \newline \newline ax2 = ax.twinx() \# same x-axis but different y-axis \newline ax2.plot(time\_variable, variable2, color = "red") \newline ax2.set\_xlabel() \newline ax2.set\_ylabel() \newline plt.show()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Add annotations}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{\# {\bf{ First argument }} is the text of the annotation \newline \# {\bf{ xy argument }} is the xy of the dataset to which the annotation has to refer (pandas object) \newline \# {\bf{ xytext argument }} is the xy coordinate of the text \newline \# {\bf{ arrowprops }} a dictionary that defines the properties of the arrow we would like to use \newline ax.annotate("", xy = , xytext = , arrowprops = \{\}) \newline \newline \# the arguments for the arrowprops \newline https://matplotlib.org/tutorials/text/annotations.html} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Annotations are small pieces of text refering to a particular part of the graph.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Bar chart}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{fig, ax = plt.subplots() \newline ax.bar(x-axis, y-axis) \newline plt.show()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.1169 cm} x{9.1531 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{barchart}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{ax.set\_xticklabels(names)} & change the names on the x axis \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} ax.set\_ylabel("") & Change name of the y axis \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} ax.bar(x,y, bottom = , label = "")) & Important that you use the same x values, set bottom= equal to the pd.series containing the data whereon you want to stack. \tn % Row Count 10 (+ 6) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{ax.legend()} \tn % Row Count 11 (+ 1) % Row 4 \SetRowColor{LightBackground} \seqsplit{ax.set\_xticklabels(x}, rotation = 90) & If you want to turn the x labels 90 degrees. \tn % Row Count 14 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{You can keep adding bars to eachother by calling ax.bar(bottom) multiple times.However, you have to add the different series in bottom: \newline "medals{[}"Gold"{]} + medals{[}"Silver"{]}"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Histogram}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{fix, ax = plt.subplots() \newline ax.hist(x, label = "", bins = value/list, histtype = , label = "") \newline ax.set\_xlabel("") \newline ax.set\_ylabel("")} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Boxplot}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{fig, ax = plt.subplots() \newline ax.boxplot(x) \newline ax.set\_ylabel() \newline \newline \# Create multiple boxplots \newline fig, ax = plt.subplots() \newline ax.boxplot({[}mens\_rowing{[}"Height"{]}, mens\_gymnastics{[}"Height"{]}{]}) \newline ax.set\_xticklabels({[}"Rowing", "Gymnastics"{]})} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Scatterplot}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{fix, ax = plt.subplots() \newline \newline ax.scatter(x, y, color = "" , label = "") \newline \newline \# create mutiple scatters on the same plot with different colors \newline fix, ax = plt.subplots() \newline ax.scatter(x, y, color = "red", label ="") \newline ax.scatter(x, y, color = "blue", label = "") \newline ax.legend() \newline ax.set\_xlabel() \newline ax.set\_ylabel() \newline \newline \newline \# encoding a third variable by color \newline fig, ax = plt.subplots() \newline ax.scatter(x, y, c = )} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Changing plot style}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{\# This has to be before the subplots method \newline plt.style.use("ggplot") \newline \newline \# The available styles \newline https://matplotlib.org/gallery/style\_sheets/style\_sheets\_reference.html} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Sharing your visualizations}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{\# save figure \newline fig.savefig("name.png")} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}