2 回答

TA貢獻1811條經驗 獲得超5個贊
集合的自動縮放(分散生成 a PathCollection)仍然是一個未解決的問題,盡管正在討論一些解決方法的想法。
在上面的例子中,一個奇怪的 hacky 解決方案是plt.plot()在創建散點圖之前向軸添加一個空圖。
import numpy as np
import matplotlib.pyplot as plt
mu1, sigma1 = 0, 1
x1 = mu1 + sigma1 * np.random.randn(10000)
hist1, bins1 = np.histogram(x1, bins='auto', density=True)
center1 = (bins1[:-1] + bins1[1:]) / 2
mu2, sigma2 = 100, 15
x2 = mu2 + sigma2 * np.random.randn(10000)
hist2, bins2 = np.histogram(x2, bins='auto', density=True)
center2 = (bins2[:-1] + bins2[1:]) / 2
plt.subplot(2, 2, 1)
plt.plot(center1, hist1)
plt.text(2, 0.27, 'plot\n$\\mu$ = 0 \n$\\sigma$ = 1')
plt.subplot(2, 2, 2)
plt.plot() ## <== empty plot
plt.scatter(center1, hist1)
plt.text(2, 0.27, 'scatter\n$\\mu$ = 0 \n$\\sigma$ = 1')
plt.subplot(2, 2, 3)
plt.plot(center2, hist2)
plt.text(127, 0.02, 'plot\n$\\mu$ = 100 \n$\\sigma$ = 15')
plt.subplot(2, 2, 4)
plt.plot() ## <== empty plot
plt.scatter(center2, hist2)
plt.text(127, 0.02, 'scatter\n$\\mu$ = 100 \n$\\sigma$ = 15')
plt.show()
以上更多的是一個笑話,盡管它適用于這種特殊情況。更嚴肅的解決方案是創建實際數據的圖,然后直接將其刪除。這足以讓自動縮放對散點圖的數據范圍按預期工作。
import numpy as np
import matplotlib.pyplot as plt
mu1, sigma1 = 0, 1
x1 = mu1 + sigma1 * np.random.randn(10000)
hist1, bins1 = np.histogram(x1, bins='auto', density=True)
center1 = (bins1[:-1] + bins1[1:]) / 2
mu2, sigma2 = 100, 15
x2 = mu2 + sigma2 * np.random.randn(10000)
hist2, bins2 = np.histogram(x2, bins='auto', density=True)
center2 = (bins2[:-1] + bins2[1:]) / 2
plt.subplot(2, 2, 1)
plt.plot(center1, hist1)
plt.text(2, 0.27, 'plot\n$\\mu$ = 0 \n$\\sigma$ = 1')
plt.subplot(2, 2, 2)
sentinel, = plt.plot(center1, hist1) ## <== sentinel plot
sentinel.remove()
plt.scatter(center1, hist1)
plt.text(2, 0.27, 'scatter\n$\\mu$ = 0 \n$\\sigma$ = 1')
plt.subplot(2, 2, 3)
plt.plot(center2, hist2)
plt.text(127, 0.02, 'plot\n$\\mu$ = 100 \n$\\sigma$ = 15')
plt.subplot(2, 2, 4)
sentinel, = plt.plot(center2, hist2) ## <== sentinel plot
sentinel.remove()
plt.scatter(center2, hist2)
plt.text(127, 0.02, 'scatter\n$\\mu$ = 100 \n$\\sigma$ = 15')
plt.show()
最后,考慮到在大網格的情況下,無論如何您當前都需要手動調整文本的位置。因此,真正的解決方案是創建一個為每個軸調用的函數,并讓它自動完成所有操作。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredText
def plot_my_hist(mu, sigma, ax=None):
ax = ax or plt.gca()
x = mu + sigma * np.random.randn(10000)
hist, bins = np.histogram(x, bins='auto', density=True)
center = (bins[:-1] + bins[1:]) / 2
# Plot
sentinel, = ax.plot(center, hist) ## <== sentinel plot
sentinel.remove()
ax.scatter(center, hist)
# Annotation
at = AnchoredText(f'scatter\n$\\mu$ = {mu} \n$\\sigma$ = {sigma}',
loc='upper right')
ax.add_artist(at)
mus = [0, 0, 12, 12, 100, 100]
sigmas = [1, 15, 1, 15, 1, 15]
fig, axes = plt.subplots(ncols=3, nrows=2, figsize=(10,6))
for ax, mu, sigma in zip(axes.T.flat, mus, sigmas):
plot_my_hist(mu, sigma, ax=ax)
fig.tight_layout()
plt.show()

TA貢獻1844條經驗 獲得超8個贊
好吧,老實說:我不知道。我唯一能發現的是,所描述的問題似乎始于最大值低于 0.1 的圖。(即,簡單地嘗試plt.scatter(center1, hist1/10)
或plt.scatter(center2, hist2*10)
)
但是,從您的示例中,我并沒有真正得到scatter
這里的需要。
如果您喜歡自動縮放plot
和藍色圓圈 - 為什么不只是
plt.plot(center2, hist2, 'o')
...?
添加回答
舉報