Code
import numpy as np
import matplotlib.pyplot as plt
Figures don’t seem to be cross-referencing across documents. Let’s see if they work within documents.
Does the link to Figure 7.1 work?
How about Figure 7.2?
Apparently so.
Okay, so do the ggplot2 figures work in this document? Specifically, does the reference to Figure 6.8 work?
No. So, the issue is cross-document figure references.
This page provides a very basic introduction to Python and the matplotlib
plotting library.
Python is an awesome language for data science and data visualization. It is more popular than R, and it is widely used in scientific research and in industry.
I find that it has a very readable syntax, meaning that it’s relatively easy to see what well-written Python code is doing.
We start by importing the numpy
and pyplot
libraries and giving them convenient short names for future reference.
import numpy as np
import matplotlib.pyplot as plt
From https://matplotlib.org/stable/gallery/statistics/hist.html#sphx-glr-gallery-statistics-hist-py
Load components from matplotlib
.
from matplotlib import colors
from matplotlib.ticker import PercentFormatter
# Create a random number generator with a fixed seed for reproducibility
= np.random.default_rng(19680801) rng
Generate data and render it.
= 100000
N_points = 20
n_bins
# Generate two normal distributions
= rng.standard_normal(N_points)
dist1 = 0.4 * rng.standard_normal(N_points) + 5
dist2
= plt.subplots(1, 2, sharey=True, tight_layout=True)
fig, axs
# We can set the number of bins with the *bins* keyword argument.
0].hist(dist1, bins=n_bins)
axs[1].hist(dist2, bins=n_bins)
axs[
plt.show()
Violin plots are another way to depict the distribution of a single continuous variable.
The following code is copied verbatim from the following site:
https://matplotlib.org/stable/gallery/statistics/violinplot.html
# fake data
= 10 # fontsize
fs = [1, 2, 4, 5, 7, 8]
pos = [np.random.normal(0, std, size=100) for std in pos]
data
# Create a plot with 2 rows and 6 columns
= plt.subplots(nrows=2, ncols=6, figsize=(10, 4))
fig, axs
0, 0].violinplot(data, pos, points=20, widths=0.3,
axs[=True, showextrema=True, showmedians=True)
showmeans0, 0].set_title('Custom violin 1', fontsize=fs)
axs[
0, 1].violinplot(data, pos, points=40, widths=0.5,
axs[=True, showextrema=True, showmedians=True,
showmeans='silverman')
bw_method0, 1].set_title('Custom violin 2', fontsize=fs)
axs[
0, 2].violinplot(data, pos, points=60, widths=0.7, showmeans=True,
axs[=True, showmedians=True, bw_method=0.5)
showextrema0, 2].set_title('Custom violin 3', fontsize=fs)
axs[
0, 3].violinplot(data, pos, points=60, widths=0.7, showmeans=True,
axs[=True, showmedians=True, bw_method=0.5,
showextrema=[[0.1], [], [], [0.175, 0.954], [0.75], [0.25]])
quantiles0, 3].set_title('Custom violin 4', fontsize=fs)
axs[
0, 4].violinplot(data[-1:], pos[-1:], points=60, widths=0.7,
axs[=True, showextrema=True, showmedians=True,
showmeans=[0.05, 0.1, 0.8, 0.9], bw_method=0.5)
quantiles0, 4].set_title('Custom violin 5', fontsize=fs)
axs[
0, 5].violinplot(data[-1:], pos[-1:], points=60, widths=0.7,
axs[=True, showextrema=True, showmedians=True,
showmeans=[0.05, 0.1, 0.8, 0.9], bw_method=0.5, side='low')
quantiles
0, 5].violinplot(data[-1:], pos[-1:], points=60, widths=0.7,
axs[=True, showextrema=True, showmedians=True,
showmeans=[0.05, 0.1, 0.8, 0.9], bw_method=0.5, side='high')
quantiles0, 5].set_title('Custom violin 6', fontsize=fs)
axs[
1, 0].violinplot(data, pos, points=80, vert=False, widths=0.7,
axs[=True, showextrema=True, showmedians=True)
showmeans1, 0].set_title('Custom violin 7', fontsize=fs)
axs[
1, 1].violinplot(data, pos, points=100, vert=False, widths=0.9,
axs[=True, showextrema=True, showmedians=True,
showmeans='silverman')
bw_method1, 1].set_title('Custom violin 8', fontsize=fs)
axs[
1, 2].violinplot(data, pos, points=200, vert=False, widths=1.1,
axs[=True, showextrema=True, showmedians=True,
showmeans=0.5)
bw_method1, 2].set_title('Custom violin 9', fontsize=fs)
axs[
1, 3].violinplot(data, pos, points=200, vert=False, widths=1.1,
axs[=True, showextrema=True, showmedians=True,
showmeans=[[0.1], [], [], [0.175, 0.954], [0.75], [0.25]],
quantiles=0.5)
bw_method1, 3].set_title('Custom violin 10', fontsize=fs)
axs[
1, 4].violinplot(data[-1:], pos[-1:], points=200, vert=False, widths=1.1,
axs[=True, showextrema=True, showmedians=True,
showmeans=[0.05, 0.1, 0.8, 0.9], bw_method=0.5)
quantiles1, 4].set_title('Custom violin 11', fontsize=fs)
axs[
1, 5].violinplot(data[-1:], pos[-1:], points=200, vert=False, widths=1.1,
axs[=True, showextrema=True, showmedians=True,
showmeans=[0.05, 0.1, 0.8, 0.9], bw_method=0.5, side='low')
quantiles
1, 5].violinplot(data[-1:], pos[-1:], points=200, vert=False, widths=1.1,
axs[=True, showextrema=True, showmedians=True,
showmeans=[0.05, 0.1, 0.8, 0.9], bw_method=0.5, side='high')
quantiles1, 5].set_title('Custom violin 12', fontsize=fs)
axs[
for ax in axs.flat:
ax.set_yticklabels([])
"Violin Plotting Examples")
fig.suptitle(=0.4)
fig.subplots_adjust(hspace plt.show()
'_mpl-gallery')
plt.style.use(
# make data:
10)
np.random.seed(= np.random.normal((3, 5, 4), (1.25, 1.00, 1.25), (100, 3))
D
# plot
= plt.subplots()
fig, ax = ax.boxplot(D, positions=[2, 4, 6], widths=1.5, patch_artist=True,
VP =False, showfliers=False,
showmeans={"color": "white", "linewidth": 0.5},
medianprops={"facecolor": "C0", "edgecolor": "white",
boxprops"linewidth": 0.5},
={"color": "C0", "linewidth": 1.5},
whiskerprops={"color": "C0", "linewidth": 1.5})
capprops
set(xlim=(0, 8), xticks=np.arange(1, 8),
ax.=(0, 8), yticks=np.arange(1, 8))
ylim
plt.show()
Source: https://matplotlib.org/stable/plot_types/basic/bar.html#sphx-glr-plot-types-basic-bar-py
'_mpl-gallery')
plt.style.use(
# make data:
= 0.5 + np.arange(8)
x = [4.8, 5.5, 3.5, 4.6, 6.5, 6.6, 2.6, 3.0]
y
# plot
= plt.subplots()
fig, ax
=1, edgecolor="white", linewidth=0.7)
ax.bar(x, y, width
set(xlim=(0, 8), xticks=np.arange(1, 8),
ax.=(0, 8), yticks=np.arange(1, 8))
ylim
plt.show()
The following is copied verbatim from the Quarto website:
https://quarto.org/docs/get-started/hello/vscode.html
For a demonstration of a line plot on a polar axis, see Figure 7.5.
= np.arange(0, 2, 0.01)
r = 2 * np.pi * r
theta = plt.subplots(
fig, ax = {'projection': 'polar'}
subplot_kw
)
ax.plot(theta, r)0.5, 1, 1.5, 2])
ax.set_rticks([True)
ax.grid( plt.show()