我正在使用 matplotlib
中的 plt.plot
和 plt.scatter
绘制两个高斯曲线(一个以 0 为中心,另一个以 100 为中心) > 版本 2.2.3。无论出于何种原因,子图都不会针对散点
图中第二条曲线的情况自动调整绘图范围。
当然我可以手动完成——在这个简单的情况下——但实际上我拥有的是一个大网格,我不想一一设置范围。
这是怎么回事?有什么办法可以解决吗?
这是我的代码:
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.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.scatter(center2, hist2)
plt.text(127, 0.02, 'scatter\n$\\mu$ = 100 \n$\\sigma$ = 15')
plt.show()
如果有人可以提供帮助,我会很高兴,提前致谢。任何答案或评论将不胜感激。
最佳答案
集合的自动缩放(分散生成 PathCollection
)仍然是 unsolved problem ,尽管正在讨论解决方法的想法。
在上面的示例中,一个奇怪的解决方案是在创建散点之前向轴添加一个空图 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()
关于python - 散点图无法正确调整 matplotlib 中的绘图范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55799059/