Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a98b59

Browse files
committed
Added a few more tips
1 parent d924c41 commit 0a98b59

File tree

5 files changed

+101
-5
lines changed

5 files changed

+101
-5
lines changed

handout-tips.tex

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
dash_capstyle, projection, Stroke, Normal, add_axes, label, savefig,
4141
get_cmap, histtype, annotate, set_minor_formatter, tick_params,
4242
fill_betweenx, text, legend, errorbar, boxplot, hist, title, xlabel,
43-
ylabel, suptitle },
43+
ylabel, suptitle, fraction, pad, set_fontname, get_xticklabels},
4444
emphstyle = {\ttfamily\bfseries}
4545
}
4646

@@ -188,6 +188,29 @@ \subsection*{\rmfamily Combining axes}
188188
\raisebox{-0.75em}{\includegraphics[width=\linewidth]{tip-dual-axis.pdf}}
189189
\end{tabular}
190190

191+
\subsection*{\rmfamily Colorbar adjustment}
192+
You can adjust colorbar aspect when adding it.
193+
194+
\begin{tabular}{@{}m{.754\linewidth}m{.236\linewidth}}
195+
\begin{lstlisting}[belowskip=-\baselineskip]
196+
im = ax.imshow(Z)
197+
198+
cb = plt.colorbar(im,
199+
fraction=0.046, pad=0.04)
200+
cb.set_ticks([])
201+
\end{lstlisting} &
202+
\raisebox{-0.75em}{\includegraphics[width=\linewidth]{tip-colorbar.pdf}}
203+
\end{tabular}
204+
205+
\subsection*{\rmfamily Take advantage of typography}
206+
You can use a condensed face such as Roboto
207+
Condensed to save space on tick labels.
208+
\begin{lstlisting}
209+
for tick in ax.get_xticklabels():
210+
tick.set_fontname("Roboto Condensed")
211+
\end{lstlisting}
212+
\includegraphics[width=\linewidth]{tip-font-family.pdf}
213+
191214

192215
\vfill
193216
%

scripts/tip-color-range.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
X = np.random.seed(1)
1818
X = np.random.randn(1000, 4)
19-
cmap = plt.get_cmap("Blues")
20-
colors = [cmap(i) for i in [.2,.4,.6,.8]]
19+
cmap = plt.get_cmap("Oranges")
20+
colors = [cmap(i) for i in [.1,.3,.5,.7]]
2121
ax.hist(X, 2, density=True, histtype='bar', color=colors)
2222

2323
plt.savefig("../figures/tip-color-range.pdf")

scripts/tip-colorbar.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -----------------------------------------------------------------------------
2+
# Matplotlib cheat sheet
3+
# Released under the BSD License
4+
# -----------------------------------------------------------------------------
5+
6+
# Scripts to generate all the basic plots
7+
import numpy as np
8+
import matplotlib as mpl
9+
import matplotlib.pyplot as plt
10+
import matplotlib.patheffects as path_effects
11+
12+
fig = plt.figure(figsize=(2.15,2))
13+
mpl.rcParams['axes.linewidth'] = 1.5
14+
d = 0.01
15+
ax = fig.add_axes([d,d,1-2*d,1-2*d], xticks=[], yticks=[])
16+
17+
np.random.seed(1)
18+
Z = np.random.uniform(0,1,(8,8))
19+
cmap = plt.get_cmap("Oranges")
20+
im = ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=2)
21+
cb = fig.colorbar(im, fraction=0.046, pad=0.04)
22+
cb.set_ticks([])
23+
24+
plt.savefig("../figures/tip-colorbar.pdf")
25+
# plt.show()

scripts/tip-font-family.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# ----------------------------------------------------------------------------
2+
# Title: Scientific Visualisation - Python & Matplotlib
3+
# Author: Nicolas P. Rougier
4+
# License: BSD
5+
# ----------------------------------------------------------------------------
6+
import numpy as np
7+
import matplotlib.pyplot as plt
8+
import matplotlib.ticker as ticker
9+
10+
# Setup a plot such that only the bottom spine is shown
11+
def setup(ax):
12+
ax.spines['right'].set_color('none')
13+
ax.spines['left'].set_color('none')
14+
ax.yaxis.set_major_locator(ticker.NullLocator())
15+
ax.spines['top'].set_color('none')
16+
17+
ax.spines['bottom'].set_position("center")
18+
19+
ax.xaxis.set_ticks_position('bottom')
20+
ax.tick_params(which='major', width=1.00)
21+
ax.tick_params(which='major', length=5)
22+
ax.tick_params(which='minor', width=0.75)
23+
ax.tick_params(which='minor', length=2.5)
24+
ax.set_xlim(0, 5)
25+
ax.set_ylim(0, 1)
26+
ax.patch.set_alpha(0.0)
27+
28+
29+
fig = plt.figure(figsize=(5, .5))
30+
fig.patch.set_alpha(0.0)
31+
n = 1
32+
33+
fontsize = 18
34+
ax = plt.subplot(n, 1, 1)
35+
ax.tick_params(axis='both', which='minor', labelsize=6)
36+
setup(ax)
37+
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
38+
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.2))
39+
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())
40+
ax.xaxis.set_minor_formatter(ticker.ScalarFormatter())
41+
ax.tick_params(axis='x', which='minor', rotation=0)
42+
43+
for tick in ax.get_xticklabels():
44+
tick.set_fontname("Roboto Condensed")
45+
46+
plt.tight_layout()
47+
plt.savefig("../figures/tip-font-family.pdf", transparent=True)
48+
# plt.show()

scripts/tip-outline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
np.random.seed(1)
1919
Z = np.random.uniform(0,1,(8,8))
20-
cmap = plt.get_cmap("Blues")
21-
ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=1)
20+
cmap = plt.get_cmap("Oranges")
21+
ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=2)
2222

2323
text = ax.text(0.5, 0.1, "Label", transform=ax.transAxes,
2424
color=cmap(0.9), size=32, weight="bold", ha="center", va="bottom")

0 commit comments

Comments
 (0)