Skip to content

Commit

Permalink
Merge pull request matplotlib#10 from matplotlib/nicolas
Browse files Browse the repository at this point in the history
Nicolas
  • Loading branch information
rougier authored Jun 26, 2020
2 parents f5fcd38 + ff9cad5 commit 02816fc
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 6 deletions.
Binary file modified cheatsheets-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified cheatsheets-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 30 additions & 6 deletions cheatsheets.tex
Original file line number Diff line number Diff line change
Expand Up @@ -931,8 +931,24 @@
K: lower center / (.5,1.1) & L: lower left / (.1,1.1)
\end{tabular}
\end{myboxed}
%
\vspace{\fill}
%
\begin{myboxed}{Annotation connection styles \hfill
\API{https://matplotlib.org/tutorials/text/annotations.html} }
\includegraphics[width=\columnwidth]{annotation-connection-styles.pdf}
\end{myboxed}
%
\vspace{\fill}
%
\begin{myboxed}{Annotation arrow styles \hfill
\API{https://matplotlib.org/tutorials/text/annotations.html} }
\includegraphics[width=\columnwidth]{annotation-arrow-styles.pdf}
\end{myboxed}

\vfill\null \columnbreak
%
\vspace{\fill}
%

%
\begin{myboxed}{How do I …}
Expand Down Expand Up @@ -960,10 +976,14 @@
\hspace*{2.5mm}~$\rightarrow$ ax.fill\_between(X, Y+error, Y-error)\\
\textbf{… draw a rectangle?}\\
\hspace*{2.5mm}~$\rightarrow$ ax.add\_patch(plt.Rectangle((0, 0),1,1)\\
\textbf{… draw a vertical line?}\\
\hspace*{2.5mm}~$\rightarrow$ ax.axvline(x=0.5)\\
\textbf{… draw outside frame?}\\
\hspace*{2.5mm}~$\rightarrow$ ax.plot(…, clip\_on=False)\\
\textbf{… use transparency?}\\
\hspace*{2.5mm}~$\rightarrow$ ax.plot(…, alpha=0.25)\\
\textbf{… convert an RGB image into a gray image? }\\
\hspace*{2.5mm}~$\rightarrow$ gray = 0.2989*R+0.5870*G+0.1140*B\\
\textbf{… set figure background color?}\\
\hspace*{2.5mm}~$\rightarrow$ fig.patch.set\_facecolor(``grey'')\\
\textbf{… get a reversed colormap?}\\
Expand All @@ -979,14 +999,18 @@
\begin{myboxed}{Performance tips}
\smallskip
{\ttfamily \fontsize{6pt}{7pt}\selectfont
%
\textcolor{red}{scatter(X, Y) \hfill slow}\\
plot(X, Y, marker="o", ls="") \hfill fast\\
\hrule \smallskip
plot(X, Y, marker="o", ls="") \hfill fast%
\vskip.5\baselineskip
%
\textcolor{red}{for i in range(n): plot(X[i]) \hfill slow}\\
plot(sum([x+[None] for x in X],[])) \hfill fast\\
\hrule \smallskip
plot(sum([x+[None] for x in X],[])) \hfill fast%
\vskip.5\baselineskip
%
\textcolor{red}{cla(), imshow(…), canvas.draw() \hfill slow}\\
im.set\_data(…), canvas.draw() \hfill fast\smallskip
im.set\_data(…), canvas.draw() \hfill fast%
\vskip.1\baselineskip
}
\end{myboxed}
%
Expand Down
28 changes: 28 additions & 0 deletions scripts/annotation-arrow-styles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

styles = mpatches.ArrowStyle.get_styles()
def demo_con_style(ax, connectionstyle):
ax.text(.05, .95, connectionstyle.replace(",", ",\n"),
family="Source Code Pro",
transform=ax.transAxes, ha="left", va="top", size="x-small")

fig, ax = plt.subplots(figsize=(4, 2.5), frameon=False)
ax.axis("off")
for i,style in enumerate(mpatches.ArrowStyle.get_styles()):
x0, y0 = 5 + 5*(i%3), -(i//3)
x1, y1 = 1 + 5*(i%3), -(i//3)
ax.plot([x0, x1], [y0, y1], ".", color="0.25")
ax.annotate("",
xy=(x0, y0), xycoords='data',
xytext=(x1, y1), textcoords='data',
arrowprops=dict(arrowstyle=style,
color="black",
shrinkA=5, shrinkB=5,
patchA=None, patchB=None,
connectionstyle="arc3,rad=0"))
ax.text( (x1+x0)/2, y0-0.2, style,
family = "Source Code Pro", ha="center", va="top")

plt.savefig("../figures/annotation-arrow-styles.pdf")
# plt.show()
35 changes: 35 additions & 0 deletions scripts/annotation-connection-styles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import matplotlib.pyplot as plt

def demo_con_style(ax, connectionstyle):
x1, y1 = 0.3, 0.2
x2, y2 = 0.8, 0.6
ax.plot([x1, x2], [y1, y2], ".")
ax.annotate("",
xy=(x1, y1), xycoords='data',
xytext=(x2, y2), textcoords='data',
arrowprops=dict(arrowstyle="->", color="0.5",
shrinkA=5, shrinkB=5,
patchA=None, patchB=None,
connectionstyle=connectionstyle),
)
ax.text(.05, .95, connectionstyle.replace(",", ",\n"),
family="Source Code Pro",
transform=ax.transAxes, ha="left", va="top", size="x-small")

fig, axs = plt.subplots(3, 3, figsize=(5, 5))
demo_con_style(axs[0, 0], "arc3,rad=0")
demo_con_style(axs[0, 1], "arc3,rad=0.3")
demo_con_style(axs[0, 2], "angle3,angleA=0,angleB=90")
demo_con_style(axs[1, 0], "angle,angleA=-90,angleB=180,rad=0")
demo_con_style(axs[1, 1], "angle,angleA=-90,angleB=180,rad=25")
demo_con_style(axs[1, 2], "arc,angleA=-90,angleB=0,armA=0,armB=40,rad=0")
demo_con_style(axs[2, 0], "bar,fraction=0.3")
demo_con_style(axs[2, 1], "bar,fraction=-0.3")
demo_con_style(axs[2, 2], "bar,angle=180,fraction=-0.2")

for ax in axs.flat:
ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1)
fig.tight_layout(pad=0.2)

plt.savefig("../figures/annotation-connection-styles.pdf")
# plt.show()

0 comments on commit 02816fc

Please sign in to comment.