猿问

散点图未在 matplotlib 中正确调整绘图范围

我正在使用2.2.3 版plt.plot和plt.scatter在matplotlib2.2.3 版中绘制两个高斯(一个以 0 为中心,另一个以 100 为中心)。出于任何原因,对于图中第二条曲线的情况,子图不会自动调整绘图范围scatter。


当然我可以手动完成——在这个简单的例子中——但实际上我拥有的是一个大网格,我不想一个一个地设置范围。


这是怎么回事?有什么办法可以解决吗?


这是我的代码:


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()

所以输出是: 

如果有人可以提供帮助,我会很高兴,在此先感谢。任何答案或评论将不胜感激。


森栏
浏览 168回答 2
2回答

四季花海

集合的自动缩放(分散生成 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()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ## <== 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()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ## <== 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)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ## <== 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)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ## <== 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):&nbsp; &nbsp; ax = ax or plt.gca()&nbsp; &nbsp; x = mu + sigma * np.random.randn(10000)&nbsp; &nbsp; hist, bins = np.histogram(x, bins='auto', density=True)&nbsp; &nbsp; center = (bins[:-1] + bins[1:]) / 2&nbsp; &nbsp; # Plot&nbsp; &nbsp; sentinel, = ax.plot(center, hist)&nbsp; &nbsp; &nbsp; ## <== sentinel plot&nbsp; &nbsp; sentinel.remove()&nbsp; &nbsp; ax.scatter(center, hist)&nbsp; &nbsp; # Annotation&nbsp; &nbsp; at = AnchoredText(f'scatter\n$\\mu$ = {mu} \n$\\sigma$ = {sigma}',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loc='upper right')&nbsp; &nbsp; 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):&nbsp; &nbsp; plot_my_hist(mu, sigma, ax=ax)fig.tight_layout()plt.show()

婷婷同学_

好吧,老实说:我不知道。我唯一能发现的是,所描述的问题似乎始于最大值低于 0.1 的图。(即,简单地尝试plt.scatter(center1, hist1/10)或plt.scatter(center2, hist2*10))但是,从您的示例中,我并没有真正得到scatter这里的需要。如果您喜欢自动缩放plot和蓝色圆圈 - 为什么不只是plt.plot(center2,&nbsp;hist2,&nbsp;'o')...?
随时随地看视频慕课网APP

相关分类

Python
我要回答