四季花海
集合的自动缩放(分散生成 a PathCollection)仍然是一个未解决的问题,尽管正在讨论一些解决方法的想法。在上面的例子中,一个奇怪的 hacky 解决方案是plt.plot()在创建散点图之前向轴添加一个空图。import numpy as npimport matplotlib.pyplot as pltmu1, sigma1 = 0, 1x1 = mu1 + sigma1 * np.random.randn(10000)hist1, bins1 = np.histogram(x1, bins='auto', density=True)center1 = (bins1[:-1] + bins1[1:]) / 2mu2, sigma2 = 100, 15x2 = mu2 + sigma2 * np.random.randn(10000)hist2, bins2 = np.histogram(x2, bins='auto', density=True)center2 = (bins2[:-1] + bins2[1:]) / 2plt.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 plotplt.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 plotplt.scatter(center2, hist2)plt.text(127, 0.02, 'scatter\n$\\mu$ = 100 \n$\\sigma$ = 15')plt.show()以上更多的是一个笑话,尽管它适用于这种特殊情况。更严肃的解决方案是创建实际数据的图,然后直接将其删除。这足以让自动缩放对散点图的数据范围按预期工作。import numpy as npimport matplotlib.pyplot as pltmu1, sigma1 = 0, 1x1 = mu1 + sigma1 * np.random.randn(10000)hist1, bins1 = np.histogram(x1, bins='auto', density=True)center1 = (bins1[:-1] + bins1[1:]) / 2mu2, sigma2 = 100, 15x2 = mu2 + sigma2 * np.random.randn(10000)hist2, bins2 = np.histogram(x2, bins='auto', density=True)center2 = (bins2[:-1] + bins2[1:]) / 2plt.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 plotsentinel.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 plotsentinel.remove()plt.scatter(center2, hist2)plt.text(127, 0.02, 'scatter\n$\\mu$ = 100 \n$\\sigma$ = 15')plt.show()最后,考虑到在大网格的情况下,无论如何您当前都需要手动调整文本的位置。因此,真正的解决方案是创建一个为每个轴调用的函数,并让它自动完成所有操作。import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.offsetbox import AnchoredTextdef 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()