我有以下代碼段來標記雙變數資料分布上的分位數。三個紅點分別對應的分位數0.1, 0.5 and 0.9。我想添加分位數值,即0.1, 0.5 and 0.9連同它們各自的紅點。如何做到這一點matplotlib?
import numpy as np
import matplotlib.pyplot as plt
num_samples = 2000
mu = np.array([5.0, 2.0])
r = np.array([
[ 3.40, -2.75],
[ -2.75, 1.0]
])
rng = np.random.default_rng()
y = rng.multivariate_normal(mu, r, size=num_samples)
quantile_set = np.quantile(y, [0.1,0.5,0.9], axis=0)
plt.plot(y[:,0], y[:,1], 'b.', alpha=0.25)
plt.plot(quantile_set[:,0],quantile_set[:,1],'ro',ms=4.5)
plt.grid(True)

uj5u.com熱心網友回復:
一種可能的解決方案(offset使文本更接近或遠離點):
import numpy as np
import matplotlib.pyplot as plt
num_samples = 2000
mu = np.array([5.0, 2.0])
r = np.array([
[ 3.40, -2.75],
[ -2.75, 1.0]
])
rng = np.random.default_rng()
y = rng.multivariate_normal(mu, r, size=num_samples)
quantile_set = np.quantile(y, [0.1,0.5,0.9], axis=0)
plt.figure()
plt.plot(y[:,0], y[:,1], 'b.', alpha=0.25)
plt.plot(quantile_set[:,0],quantile_set[:,1],'ro',ms=4.5)
qtexts = [0.1, 0.5, 0.9]
offset = np.array([
abs(y[:, 0].min() - y[:, 0].max()) * 0.05,
abs(y[:, 1].min() - y[:, 1].max()) * 0.05
])
for q, t in zip(quantile_set, qtexts):
plt.text(*(q offset), t, color="r", horizontalalignment="right", verticalalignment="top")
plt.grid(True)

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/468455.html
