Skip to content

Commit

Permalink
Added a few more tips
Browse files Browse the repository at this point in the history
  • Loading branch information
rougier committed Jun 30, 2020
1 parent d924c41 commit 0a98b59
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 5 deletions.
25 changes: 24 additions & 1 deletion handout-tips.tex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
dash_capstyle, projection, Stroke, Normal, add_axes, label, savefig,
get_cmap, histtype, annotate, set_minor_formatter, tick_params,
fill_betweenx, text, legend, errorbar, boxplot, hist, title, xlabel,
ylabel, suptitle },
ylabel, suptitle, fraction, pad, set_fontname, get_xticklabels},
emphstyle = {\ttfamily\bfseries}
}

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

\subsection*{\rmfamily Colorbar adjustment}
You can adjust colorbar aspect when adding it.

\begin{tabular}{@{}m{.754\linewidth}m{.236\linewidth}}
\begin{lstlisting}[belowskip=-\baselineskip]
im = ax.imshow(Z)

cb = plt.colorbar(im,
fraction=0.046, pad=0.04)
cb.set_ticks([])
\end{lstlisting} &
\raisebox{-0.75em}{\includegraphics[width=\linewidth]{tip-colorbar.pdf}}
\end{tabular}

\subsection*{\rmfamily Take advantage of typography}
You can use a condensed face such as Roboto
Condensed to save space on tick labels.
\begin{lstlisting}
for tick in ax.get_xticklabels():
tick.set_fontname("Roboto Condensed")
\end{lstlisting}
\includegraphics[width=\linewidth]{tip-font-family.pdf}


\vfill
%
Expand Down
4 changes: 2 additions & 2 deletions scripts/tip-color-range.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

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

plt.savefig("../figures/tip-color-range.pdf")
Expand Down
25 changes: 25 additions & 0 deletions scripts/tip-colorbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -----------------------------------------------------------------------------
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------

# Scripts to generate all the basic plots
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects

fig = plt.figure(figsize=(2.15,2))
mpl.rcParams['axes.linewidth'] = 1.5
d = 0.01
ax = fig.add_axes([d,d,1-2*d,1-2*d], xticks=[], yticks=[])

np.random.seed(1)
Z = np.random.uniform(0,1,(8,8))
cmap = plt.get_cmap("Oranges")
im = ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=2)
cb = fig.colorbar(im, fraction=0.046, pad=0.04)
cb.set_ticks([])

plt.savefig("../figures/tip-colorbar.pdf")
# plt.show()
48 changes: 48 additions & 0 deletions scripts/tip-font-family.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# ----------------------------------------------------------------------------
# Title: Scientific Visualisation - Python & Matplotlib
# Author: Nicolas P. Rougier
# License: BSD
# ----------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

# Setup a plot such that only the bottom spine is shown
def setup(ax):
ax.spines['right'].set_color('none')
ax.spines['left'].set_color('none')
ax.yaxis.set_major_locator(ticker.NullLocator())
ax.spines['top'].set_color('none')

ax.spines['bottom'].set_position("center")

ax.xaxis.set_ticks_position('bottom')
ax.tick_params(which='major', width=1.00)
ax.tick_params(which='major', length=5)
ax.tick_params(which='minor', width=0.75)
ax.tick_params(which='minor', length=2.5)
ax.set_xlim(0, 5)
ax.set_ylim(0, 1)
ax.patch.set_alpha(0.0)


fig = plt.figure(figsize=(5, .5))
fig.patch.set_alpha(0.0)
n = 1

fontsize = 18
ax = plt.subplot(n, 1, 1)
ax.tick_params(axis='both', which='minor', labelsize=6)
setup(ax)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.2))
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())
ax.xaxis.set_minor_formatter(ticker.ScalarFormatter())
ax.tick_params(axis='x', which='minor', rotation=0)

for tick in ax.get_xticklabels():
tick.set_fontname("Roboto Condensed")

plt.tight_layout()
plt.savefig("../figures/tip-font-family.pdf", transparent=True)
# plt.show()
4 changes: 2 additions & 2 deletions scripts/tip-outline.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

np.random.seed(1)
Z = np.random.uniform(0,1,(8,8))
cmap = plt.get_cmap("Blues")
ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=1)
cmap = plt.get_cmap("Oranges")
ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=2)

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

0 comments on commit 0a98b59

Please sign in to comment.