Skip to content

Commit 02816fc

Browse files
authored
Merge pull request #10 from matplotlib/nicolas
Nicolas
2 parents f5fcd38 + ff9cad5 commit 02816fc

5 files changed

+93
-6
lines changed

cheatsheets-1.png

0 Bytes
Loading

cheatsheets-2.png

37.4 KB
Loading

cheatsheets.tex

+30-6
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,24 @@
931931
K: lower center / (.5,1.1) & L: lower left / (.1,1.1)
932932
\end{tabular}
933933
\end{myboxed}
934+
%
935+
\vspace{\fill}
936+
%
937+
\begin{myboxed}{Annotation connection styles \hfill
938+
\API{https://matplotlib.org/tutorials/text/annotations.html} }
939+
\includegraphics[width=\columnwidth]{annotation-connection-styles.pdf}
940+
\end{myboxed}
941+
%
942+
\vspace{\fill}
943+
%
944+
\begin{myboxed}{Annotation arrow styles \hfill
945+
\API{https://matplotlib.org/tutorials/text/annotations.html} }
946+
\includegraphics[width=\columnwidth]{annotation-arrow-styles.pdf}
947+
\end{myboxed}
934948

935-
\vfill\null \columnbreak
949+
%
950+
\vspace{\fill}
951+
%
936952

937953
%
938954
\begin{myboxed}{How do I …}
@@ -960,10 +976,14 @@
960976
\hspace*{2.5mm}~$\rightarrow$ ax.fill\_between(X, Y+error, Y-error)\\
961977
\textbf{… draw a rectangle?}\\
962978
\hspace*{2.5mm}~$\rightarrow$ ax.add\_patch(plt.Rectangle((0, 0),1,1)\\
979+
\textbf{… draw a vertical line?}\\
980+
\hspace*{2.5mm}~$\rightarrow$ ax.axvline(x=0.5)\\
963981
\textbf{… draw outside frame?}\\
964982
\hspace*{2.5mm}~$\rightarrow$ ax.plot(…, clip\_on=False)\\
965983
\textbf{… use transparency?}\\
966984
\hspace*{2.5mm}~$\rightarrow$ ax.plot(…, alpha=0.25)\\
985+
\textbf{… convert an RGB image into a gray image? }\\
986+
\hspace*{2.5mm}~$\rightarrow$ gray = 0.2989*R+0.5870*G+0.1140*B\\
967987
\textbf{… set figure background color?}\\
968988
\hspace*{2.5mm}~$\rightarrow$ fig.patch.set\_facecolor(``grey'')\\
969989
\textbf{… get a reversed colormap?}\\
@@ -979,14 +999,18 @@
979999
\begin{myboxed}{Performance tips}
9801000
\smallskip
9811001
{\ttfamily \fontsize{6pt}{7pt}\selectfont
1002+
%
9821003
\textcolor{red}{scatter(X, Y) \hfill slow}\\
983-
plot(X, Y, marker="o", ls="") \hfill fast\\
984-
\hrule \smallskip
1004+
plot(X, Y, marker="o", ls="") \hfill fast%
1005+
\vskip.5\baselineskip
1006+
%
9851007
\textcolor{red}{for i in range(n): plot(X[i]) \hfill slow}\\
986-
plot(sum([x+[None] for x in X],[])) \hfill fast\\
987-
\hrule \smallskip
1008+
plot(sum([x+[None] for x in X],[])) \hfill fast%
1009+
\vskip.5\baselineskip
1010+
%
9881011
\textcolor{red}{cla(), imshow(…), canvas.draw() \hfill slow}\\
989-
im.set\_data(…), canvas.draw() \hfill fast\smallskip
1012+
im.set\_data(…), canvas.draw() \hfill fast%
1013+
\vskip.1\baselineskip
9901014
}
9911015
\end{myboxed}
9921016
%

scripts/annotation-arrow-styles.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import matplotlib.pyplot as plt
2+
import matplotlib.patches as mpatches
3+
4+
styles = mpatches.ArrowStyle.get_styles()
5+
def demo_con_style(ax, connectionstyle):
6+
ax.text(.05, .95, connectionstyle.replace(",", ",\n"),
7+
family="Source Code Pro",
8+
transform=ax.transAxes, ha="left", va="top", size="x-small")
9+
10+
fig, ax = plt.subplots(figsize=(4, 2.5), frameon=False)
11+
ax.axis("off")
12+
for i,style in enumerate(mpatches.ArrowStyle.get_styles()):
13+
x0, y0 = 5 + 5*(i%3), -(i//3)
14+
x1, y1 = 1 + 5*(i%3), -(i//3)
15+
ax.plot([x0, x1], [y0, y1], ".", color="0.25")
16+
ax.annotate("",
17+
xy=(x0, y0), xycoords='data',
18+
xytext=(x1, y1), textcoords='data',
19+
arrowprops=dict(arrowstyle=style,
20+
color="black",
21+
shrinkA=5, shrinkB=5,
22+
patchA=None, patchB=None,
23+
connectionstyle="arc3,rad=0"))
24+
ax.text( (x1+x0)/2, y0-0.2, style,
25+
family = "Source Code Pro", ha="center", va="top")
26+
27+
plt.savefig("../figures/annotation-arrow-styles.pdf")
28+
# plt.show()
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import matplotlib.pyplot as plt
2+
3+
def demo_con_style(ax, connectionstyle):
4+
x1, y1 = 0.3, 0.2
5+
x2, y2 = 0.8, 0.6
6+
ax.plot([x1, x2], [y1, y2], ".")
7+
ax.annotate("",
8+
xy=(x1, y1), xycoords='data',
9+
xytext=(x2, y2), textcoords='data',
10+
arrowprops=dict(arrowstyle="->", color="0.5",
11+
shrinkA=5, shrinkB=5,
12+
patchA=None, patchB=None,
13+
connectionstyle=connectionstyle),
14+
)
15+
ax.text(.05, .95, connectionstyle.replace(",", ",\n"),
16+
family="Source Code Pro",
17+
transform=ax.transAxes, ha="left", va="top", size="x-small")
18+
19+
fig, axs = plt.subplots(3, 3, figsize=(5, 5))
20+
demo_con_style(axs[0, 0], "arc3,rad=0")
21+
demo_con_style(axs[0, 1], "arc3,rad=0.3")
22+
demo_con_style(axs[0, 2], "angle3,angleA=0,angleB=90")
23+
demo_con_style(axs[1, 0], "angle,angleA=-90,angleB=180,rad=0")
24+
demo_con_style(axs[1, 1], "angle,angleA=-90,angleB=180,rad=25")
25+
demo_con_style(axs[1, 2], "arc,angleA=-90,angleB=0,armA=0,armB=40,rad=0")
26+
demo_con_style(axs[2, 0], "bar,fraction=0.3")
27+
demo_con_style(axs[2, 1], "bar,fraction=-0.3")
28+
demo_con_style(axs[2, 2], "bar,angle=180,fraction=-0.2")
29+
30+
for ax in axs.flat:
31+
ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1)
32+
fig.tight_layout(pad=0.2)
33+
34+
plt.savefig("../figures/annotation-connection-styles.pdf")
35+
# plt.show()

0 commit comments

Comments
 (0)