散点图无法在matplotlib中正确调整散点图范围

时间:2019-04-22 18:17:11

标签: python matplotlib scatter-plot pad

我正在用plt.plot 2.2.3版中的plt.scattermatplotlib绘制两个高斯(一个居中于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()

因此输出为: enter image description here

如果有人可以提供帮助,我将感到非常高兴,在此先感谢您。任何答复或评论将不胜感激。

2 个答案:

答案 0 :(得分:2)

自动缩放集合(散布会产生PathCollection)仍然是unsolved problem,尽管有讨论变通办法的想法。

在上述示例中,一种奇怪的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()

enter image description here

尽管在特定情况下有效,但以上内容只是一个笑话。更为严重的解决方案是创建实际数据图,然后直接将其删除。这足以让自动缩放功能在散点图的数据范围内正常工作。

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

enter image description here

最后,请考虑到在大网格图的情况下,当前您仍然需要手动调整文本的位置。因此,真正的解决方案是创建一个为每个轴调用的函数,然后让该函数自动执行所有操作。

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

enter image description here

答案 1 :(得分:1)

好的,说实话:我不知道。我唯一能发现的是,所描述的问题似乎始于最大值低于0.1的图。 (即,只需尝试<?xml version="1.0" encoding="UTF-8"?> <Response> <Dial> <Sip> alex@something.sip.us1.twilio.com </Sip> </Dial> </Response> plt.scatter(center1, hist1/10)

但是,在您的示例中,我并没有真正需要plt.scatter(center2, hist2*10)
如果您喜欢scatter以及蓝色圆圈的自动缩放-为什么不只是

plot

...?