Skip to content

Commit eae75bd

Browse files
committed
Added legend section
1 parent a32f218 commit eae75bd

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

handout-intermediate.tex

+29-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
%
3737
emph = { plot, scatter, imshow, bar, contourf, pie, subplots, spines,
3838
add_gridspec, add_subplot, set_xscale, set_minor_locator,
39-
set_minor_formatter, tick_params, fill_betweenx, text,
39+
set_minor_formatter, tick_params, fill_betweenx, text, legend,
4040
errorbar, boxplot, hist, title, xlabel, ylabel, suptitle },
4141
emphstyle = {\ttfamily\bfseries}
4242
}
@@ -78,7 +78,7 @@ \section*{\LARGE \rmfamily
7878

7979
\includegraphics[width=\linewidth]{anatomy-cropped.pdf}
8080

81-
\subsection*{Figure, axes \& spines}
81+
\subsection*{\rmfamily Figure, axes \& spines}
8282

8383
% -----------------------------------------------------------------------------
8484
\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}}
@@ -113,7 +113,7 @@ \subsection*{Figure, axes \& spines}
113113

114114

115115
% -----------------------------------------------------------------------------
116-
\subsection*{Ticks \& labels}
116+
\subsection*{\rmfamily Ticks \& labels}
117117

118118
\begin{lstlisting}[basicstyle=\ttfamily\small]
119119
from mpl.ticker import MultipleLocator as ML
@@ -125,7 +125,7 @@ \subsection*{Ticks \& labels}
125125
\includegraphics[width=\linewidth]{tick-multiple-locator.pdf}
126126

127127
% -----------------------------------------------------------------------------
128-
\subsection*{Lines \& markers}
128+
\subsection*{\rmfamily Lines \& markers}
129129

130130
\begin{lstlisting}
131131
X = np.linspace(0.1, 10*np.pi, 1000)
@@ -135,7 +135,7 @@ \subsection*{Lines \& markers}
135135
\includegraphics[width=\linewidth]{sine-marker.pdf}
136136

137137
% -----------------------------------------------------------------------------
138-
\subsection*{Scales \& Projections}
138+
\subsection*{\rmfamily Scales \& Projections}
139139

140140
\begin{lstlisting}
141141
fig, ax = plt.subplots()
@@ -144,7 +144,7 @@ \subsection*{Scales \& Projections}
144144
\end{lstlisting}
145145
\includegraphics[width=\linewidth]{sine-logscale.pdf}
146146

147-
\subsection*{Text \& Ornaments}
147+
\subsection*{\rmfamily Text \& Ornaments}
148148
\begin{lstlisting}[]
149149
ax.fill_betweenx([-1,1],[0],[2*np.pi])
150150
ax.text(0, -1, r" Period $\Phi$")
@@ -153,12 +153,33 @@ \subsection*{Text \& Ornaments}
153153

154154

155155
% -----------------------------------------------------------------------------
156-
\subsection*{Size \& DPI}
156+
\subsection*{\rmfamily Legend}
157+
\begin{lstlisting}[]
158+
ax.plot(X, np.sin(X), "C0", label="Sine")
159+
ax.plot(X, np.cos(X), "C1", label="Cosine")
160+
ax.legend(bbox_to_anchor=(0,1,1,.1),ncol=2,
161+
mode="expand", loc="lower left")
162+
\end{lstlisting}
163+
\includegraphics[width=\linewidth]{sine-legend.pdf}
164+
165+
166+
% -----------------------------------------------------------------------------
167+
\subsection*{\rmfamily Colors}
168+
169+
Any color can be used but Matplotlib offers a set of colors:\\
170+
\includegraphics[width=\linewidth]{colors-cycle.pdf} \smallskip
171+
\includegraphics[width=\linewidth]{colors-grey.pdf}\\
172+
As well as nice colormaps (viridis an magma):\\
173+
\includegraphics[width=\linewidth]{colormap-viridis.pdf} \smallskip
174+
\includegraphics[width=\linewidth]{colormap-magma.pdf} \medskip
175+
176+
% -----------------------------------------------------------------------------
177+
\subsection*{\rmfamily Size \& DPI}
157178

158179
Consider a square figure to be included in a two-columns A4 paper with
159180
2cm margins on each side and a column separation of 1cm. The width of
160181
a figure is (21 - 2*2 - 1)/2 = 8cm. One inch being 2.54cm, figure size
161-
should be 3.15$\times$3.15 inches.
182+
should be 3.15$\times$3.15 in.
162183
\begin{lstlisting}[]
163184
fig = plt.figure(figsize=(3.15,3.15), dpi=50)
164185
plt.savefig("figure.pdf", dpi=600)

scripts/sine.py

+19-18
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,27 @@
3131
# plt.savefig("../figures/sine-logscale.pdf", dpi=100)
3232

3333

34+
# fig = plt.figure(figsize=(7,1.5))
35+
# plt.plot(X, Y, "C1", lw=2)
36+
# plt.fill_betweenx([-1.5,1.5],[0],[2*np.pi], color=".9")
37+
# plt.text(0, -1, r" Period $\Phi$")
38+
# # plt.xticks([]), plt.yticks([])
39+
# plt.ylim(-1.5, 1.5)
40+
# plt.tight_layout()
41+
# plt.savefig("../figures/sine-period.pdf", dpi=100)
42+
# plt.show()
43+
44+
3445
fig = plt.figure(figsize=(7,1.5))
35-
plt.plot(X, Y, "C1", lw=2)
36-
plt.fill_betweenx([-1.5,1.5],[0],[2*np.pi], color=".9")
37-
plt.text(0, -1, r" Period $\Phi$")
38-
# plt.xticks([]), plt.yticks([])
39-
plt.ylim(-1.5, 1.5)
46+
plt.plot(X, np.sin(X), "C0", lw=2, label="Sine")
47+
plt.plot(X, np.cos(X), "C1", lw=2, label="Cosine")
48+
plt.legend(bbox_to_anchor = (0.0, .9, 1.02, 0.1),
49+
frameon=False, mode="expand", ncol=2, loc="lower left")
50+
plt.title("Sine and Cosine")
51+
plt.xticks([]), plt.yticks([])
52+
plt.ylim(-1.25, 1.25)
4053
plt.tight_layout()
41-
plt.savefig("../figures/sine-period.pdf", dpi=100)
54+
plt.savefig("../figures/sine-legend.pdf", dpi=100)
4255
plt.show()
4356

44-
# X = np.linspace(0, 2*np.pi, 10000)
45-
# Y = np.sin(10*X)
4657

47-
# fig = plt.figure(figsize=(3,3))
48-
# ax = plt.subplot(projection="polar")
49-
# ax.set_rorigin(-3)
50-
# ax.set_xticks(np.linspace(0, 2*np.pi, 8+1 ))
51-
# ax.set_xticklabels([])
52-
# ax.set_yticklabels([])
53-
# plt.ylim(-1.5, 1.5)
54-
# plt.plot(X, Y, "C1")
55-
# plt.savefig("../figures/sine-polar.pdf", dpi=100)
56-
# plt.show()

0 commit comments

Comments
 (0)