Ch. 8 Advanced
Last update: Thu Nov 5 19:22:01 2020 -0600 (5124cef)
R
library(reticulate)
reticulate::use_condaenv("r-python")
8.1 Adding labels
Math symbols printing with Latex.
\(r's(t) = \mathcal{A}\/\sin(2 \omega t)'\)
\(\mathbb{blackboard}\)
Python
import matplotlib.pyplot as plt
import numpy as np
= 100, 15
mu, sigma = mu + sigma * np.random.randn(10000)
x
# the histogram of the data
= plt.hist(x, 50, density=1, facecolor='g', alpha=0.75)
n, bins, patches
'Smarts')
plt.xlabel('Probability')
plt.ylabel('Histogram of IQ')
plt.title(60, .025, r'$\mu=100,\ \sigma=15$')
plt.text(40, 160, 0, 0.03])
plt.axis([True)
plt.grid( plt.show()
Python
# http://scipy-lectures.org/_downloads/ScipyLectures-simple.pdf
import numpy as np
import matplotlib.pyplot as plt
= []
eqs
r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = U^{3\beta}_{\delta_1 \rho_1} + \frac{1} {8 \pi 2} \int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_{\rho_1 \sigma_2}}{U^{0\beta}_{\rho_1 \sigma_2}} \right]$")) # eq1
eqs.append((r"$\frac{d\rho}{d t} + \rho \vec{v}\cdot\nabla\vec{v} = -\nabla p + \mu\nabla^2 \vec{v} + \rho \vec{g}$")) # eq2
eqs.append((r"$\int_{-\infty}^\infty e^{-x^2}dx=\sqrt{\pi}$")) # eq3
eqs.append((r"$E = mc^2 = \sqrt{ {m_0}^2 c^4 + p^2 c^2}$")) # eq4
eqs.append((r"$F_G = G\frac{m_1m_2}{r^2}$")) # eq5
eqs.append((
0.025, 0.025, 0.95, 0.95])
plt.axes([
for i in range(24):
= np.random.randint(0, len(eqs))
index = eqs[index]
eq = np.random.uniform(12, 32)
size = np.random.uniform(0, 1, 2)
x,y = np.random.uniform(0.25, .75)
alpha ='center', va='center', color="#11557c", alpha=alpha,transform=plt.gca().transAxes, fontsize=size, clip_on=True)
plt.text(x, y, eq, ha
plt.xticks([])
plt.yticks([]) plt.show()
8.2 Subplots
Python
# https://matplotlib.org/gallery/subplots_axes_and_figures/subplots_adjust.html#sphx-glr-gallery-subplots-axes-and-figures-subplots-adjust-py
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
19680801)
np.random.seed(
211)
plt.subplot(100, 100)), cmap=plt.cm.BuPu_r)
plt.imshow(np.random.random((212)
plt.subplot(100, 100)), cmap=plt.cm.BuPu_r)
plt.imshow(np.random.random((
=0.1, right=0.8, top=0.9)
plt.subplots_adjust(bottom= plt.axes([0.85, 0.1, 0.075, 0.8])
cax =cax)
plt.colorbar(cax plt.show()
Python
# https://matplotlib.org/gallery/text_labels_and_annotations/figlegend_demo.html#sphx-glr-gallery-text-labels-and-annotations-figlegend-demo-py
import numpy as np
import matplotlib.pyplot as plt
= plt.subplots(1, 2)
fig, axs
= np.arange(0.0, 2.0, 0.02)
x = np.sin(2 * np.pi * x)
y1 = np.exp(-x)
y2 = axs[0].plot(x, y1, 'rs-', x, y2, 'go')
l1, l2
= np.sin(4 * np.pi * x)
y3 = np.exp(-2 * x)
y4 = axs[1].plot(x, y3, 'yd-', x, y4, 'k^')
l3, l4
'Line 1', 'Line 2'), 'upper left')
fig.legend((l1, l2), ('Line 3', 'Line 4'), 'upper right')
fig.legend((l3, l4), (
plt.tight_layout() plt.show()
Python
import matplotlib.pyplot as plt
import numpy as np
= ['group_a', 'group_b', 'group_c']
names = [1, 10, 100]
values
=(9, 3))
plt.figure(figsize
131)
plt.subplot(
plt.bar(names, values)132)
plt.subplot(
plt.scatter(names, values)133)
plt.subplot(
plt.plot(names, values)'Categorical Plotting')
plt.suptitle( plt.show()
Python
# https://www.python-course.eu/matplotlib_multiple_figures.php
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
=(6, 4))
plt.figure(figsize= gridspec.GridSpec(3, 3)
G = np.linspace(0, 2 * np.pi, 50, endpoint=True)
X = 2.8 * np.cos(X)
F1 = 5 * np.sin(X)
F2 = 0.3 * np.sin(X)
F3 = plt.subplot(G[0, :])
axes_1 'r-', X, F2)
axes_1.plot(X, F1, = plt.subplot(G[1, :-1])
axes_2
axes_2.plot(X, F3)= plt.subplot(G[1:, -1])
axes_3 1,2,3,4], [1,10,100,1000], 'b-')
axes_3.plot([= plt.subplot(G[-1, 0])
axes_4 1,2,3,4], [47, 11, 42, 60], 'r-')
axes_4.plot([= plt.subplot(G[-1, -2])
axes_5 1,2,3,4], [7, 5, 4, 3.8])
axes_5.plot([
plt.tight_layout() plt.show()
Python
# https://matplotlib.org/gallery/text_labels_and_annotations/figlegend_demo.html#sphx-glr-gallery-text-labels-and-annotations-figlegend-demo-py
import numpy as np
import matplotlib.pyplot as plt
= plt.subplots(1, 2)
fig, axs
= np.arange(0.0, 2.0, 0.02)
x = np.sin(2 * np.pi * x)
y1 = np.exp(-x)
y2 = axs[0].plot(x, y1, 'rs-', x, y2, 'go')
l1, l2
= np.sin(4 * np.pi * x)
y3 = np.exp(-2 * x)
y4 = axs[1].plot(x, y3, 'yd-', x, y4, 'k^')
l3, l4
'Line 1', 'Line 2'), 'upper left')
fig.legend((l1, l2), ('Line 3', 'Line 4'), 'upper right')
fig.legend((l3, l4), (
plt.tight_layout() plt.show()
8.3 Change of axes
Python
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
19680801)
np.random.seed(
# make up some data in the open interval (0, 1)
= np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y
y.sort()= np.arange(len(y))
x
# plot with various axes scales
plt.figure()
# linear
221)
plt.subplot(
plt.plot(x, y)'linear')
plt.yscale('linear')
plt.title(True)
plt.grid(
# log
222)
plt.subplot(
plt.plot(x, y)'log')
plt.yscale('log')
plt.title(True)
plt.grid(
# symmetric log
223)
plt.subplot(- y.mean())
plt.plot(x, y 'symlog', linthresh=0.01)
plt.yscale('symlog')
plt.title(True)
plt.grid(
# logit
224)
plt.subplot(
plt.plot(x, y)'logit')
plt.yscale('logit')
plt.title(True)
plt.grid(# Adjust the subplot layout, because the logit one may take more space
# than usual, due to y-tick labels like "1 - 10^{-3}"
=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
plt.subplots_adjust(top=0.35)
wspace
plt.show()