Skip to content

Commit 3c2520b

Browse files
committed
Added colors & legend
1 parent eeb1719 commit 3c2520b

File tree

6 files changed

+159
-5
lines changed

6 files changed

+159
-5
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
# cheatsheets
2-
Various cheat sheets for matplotlib
1+
# Cheatsheets
2+
3+
![](./cheatsheets-1.png)
4+
5+
![](./cheatsheets-2.png)
36

47
# How to compile
58

@@ -9,6 +12,7 @@ Various cheat sheets for matplotlib
912
* `fonts/source-code-pro/*` : See https://fonts.google.com/specimen/Source+Code+Pro
1013
* `fonts/source-sans-pro/*` : See https://fonts.google.com/specimen/Source+Sans+Pro
1114
* `fonts/source-serif-pro/*` : See https://fonts.google.com/specimen/Source+Serif+Pro
15+
* `fonts/delicious-123: See https://www.exljbris.com/delicious.html
1216

1317
2. You need to generate all the figures:
1418

@@ -26,3 +30,4 @@ $ xelatex cheatsheet-basic.tex
2630

2731

2832

33+

cheatsheets-1.png

671 KB
Loading

cheatsheets-2.png

380 KB
Loading

cheatsheet-basic.tex renamed to cheatsheets.tex

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,9 +875,51 @@
875875
\colormap{twilight}
876876
\end{tabular}
877877
\end{myboxed}
878-
879-
\end{multicols*}
880-
881878

879+
\begin{myboxed}{Color names}
880+
\includegraphics[width=\columnwidth]{colornames.pdf}
881+
\end{myboxed}
882+
%
883+
\vspace{\fill}
884+
%
885+
\begin{myboxed}{Legend placement}
886+
\includegraphics[width=\columnwidth]{legend-placement.pdf}
887+
plt.\textbf{legend}(loc="string", bbox\_to\_anchor=(x,y))\\
888+
\begin{tabular}{@{}p{0.33\columnwidth}
889+
p{0.33\columnwidth}
890+
p{0.33\columnwidth}@{}}
891+
\scriptsize \rule{0pt}{1.25em}\noindent
892+
1: lower left & 2: lower center & 3: lower right\\
893+
4: left & 5: center & 6: right\\
894+
7: upper left & 8: upper center & 9: upper right\\
895+
\end{tabular}
896+
897+
\begin{tabular}{@{}p{0.495\columnwidth}
898+
p{0.495\columnwidth}@{}}
899+
\scriptsize \rule{0pt}{1.25em}\noindent
900+
A: upper right / (-.1,.9) & B: right / (-.1,.5)\\
901+
C: lower right / (-.1,.1) & D: upper left / (-.1,-.1)\\
902+
E: upper center / (.5,-.1) & F: upper right / (.9,-.1)\\
903+
G: lower left / (1.1,.1) & H: left / (1.1,.5)\\
904+
I: upper left / (1.1,.9) & J: lower right / (.9,1.1)\\
905+
K: lower center / (.5,1.1) & L: lower left / (.1,1.1)
906+
\end{tabular}
907+
908+
\end{myboxed}
909+
%
910+
\vspace{\fill}
911+
%
912+
\begin{myboxed}{How do I …}
913+
\textbf{… resize a figure?}\\
914+
\hspace*{2.5mm}~$\rightarrow$ fig.set\_size\_inches(w,h)\\
915+
\textbf{… save a figure?}\\
916+
\hspace*{2.5mm}~$\rightarrow$ plt.savefig("figure.pdf")\\
917+
\textbf{… clear a figure?}\\
918+
\hspace*{2.5mm}~$\rightarrow$ ax.clear()\\
919+
\textbf{… close all figures?}\\
920+
\hspace*{2.5mm}~$\rightarrow$ plt.close("all")
921+
\end{myboxed}
922+
923+
\end{multicols*}
882924
\end{document}
883925

scripts/colornames.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
========================
3+
Visualizing named colors
4+
========================
5+
6+
Simple plot example with the named colors and its visual representation.
7+
"""
8+
from __future__ import division
9+
10+
import matplotlib.pyplot as plt
11+
from matplotlib import colors as mcolors
12+
13+
14+
colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)
15+
16+
# Sort colors by hue, saturation, value and name.
17+
by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
18+
for name, color in colors.items())
19+
sorted_names = [name for hsv, name in by_hsv]
20+
21+
n = len(sorted_names)
22+
ncols = 3
23+
nrows = n // ncols + 1
24+
25+
fig, ax = plt.subplots(figsize=(4.5, 6))
26+
27+
# Get height and width
28+
X, Y = fig.get_dpi() * fig.get_size_inches()
29+
h = Y / (nrows + 1)
30+
w = X / ncols
31+
32+
for i, name in enumerate(sorted_names):
33+
col = i // nrows
34+
row = i % nrows
35+
y = Y - (row * h) - h
36+
37+
xi_line = w * (col + 0.05)
38+
xf_line = w * (col + 0.25)
39+
xi_text = w * (col + 0.3)
40+
41+
ax.text(xi_text, y, name, fontsize=7,
42+
horizontalalignment='left',
43+
verticalalignment='center')
44+
45+
ax.hlines(y + h * 0.1, xi_line, xf_line,
46+
color=colors[name], linewidth=(h * 0.6))
47+
48+
ax.set_xlim(0, X)
49+
ax.set_ylim(0, Y)
50+
ax.set_axis_off()
51+
52+
fig.subplots_adjust(left=0, right=1,
53+
top=1, bottom=0,
54+
hspace=0, wspace=0)
55+
56+
plt.savefig("../figures/colornames.pdf")
57+
# plt.show()

scripts/legend.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -----------------------------------------------------------------------------
2+
# Matplotlib cheat sheet
3+
# Released under the BSD License
4+
# -----------------------------------------------------------------------------
5+
import numpy as np
6+
import matplotlib as mpl
7+
import matplotlib.pyplot as plt
8+
9+
10+
fig = plt.figure(figsize=(4,4))
11+
ax = fig.add_axes([0.15,0.15,.7,.7], frameon=True, aspect=1,
12+
xticks=[], yticks=[])
13+
14+
def text(x, y, _text):
15+
color= "C1"
16+
if not 0 < x < 1 or not 0 < y < 1: color = "C0"
17+
size = 0.15
18+
ax.text(x, y, _text, color="white", #bbox={"color": "C1"},
19+
size="xx-large", weight="bold", ha="center", va="center")
20+
rect = plt.Rectangle((x-size/2, y-size/2), size, size, facecolor=color,
21+
zorder=-10, clip_on=False)
22+
ax.add_patch(rect)
23+
24+
def point(x, y):
25+
ax.scatter([x], [y], facecolor="C0", edgecolor="white",
26+
zorder=10, clip_on=False)
27+
28+
d = .1
29+
e = .15/2
30+
31+
text( d, d, "1"), text( 0.5, d, "2"), text(1-d, d, "3")
32+
text( d, 0.5, "4"), text( 0.5, 0.5, "5"), text(1-d, 0.5, "6")
33+
text( d, 1-d, "7"), text( 0.5, 1-d, "8"), text(1-d, 1-d, "9")
34+
35+
text( -d, 1-d, "A"), text( -d, 0.5, "B"), text( -d, d, "C")
36+
point(-d+e, 1-d+e), point(-d+e, 0.5), point(-d+e, d-e),
37+
38+
text( d, -d, "D"), text(0.5, -d, "E"), text( 1-d, -d, "F")
39+
point(d-e, -d+e), point(0.5, -d+e), point(1-d+e, -d+e),
40+
41+
text(1+d, d, "G"), text(1+d, 0.5, "H"), text( 1+d, 1-d, "I")
42+
point(1+d-e, d-e), point(1+d-e, .5), point(1+d-e, 1-d+e),
43+
44+
text(1-d, 1+d, "J"), text(0.5, 1+d, "K"), text( d, 1+d, "L")
45+
point(1-d+e, 1+d-e), point(0.5, 1+d-e), point(d-e, 1+d-e),
46+
47+
plt.xlim(0,1), plt.ylim(0,1)
48+
49+
plt.savefig("../figures/legend-placement.pdf")
50+
# plt.show()

0 commit comments

Comments
 (0)