亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

散點圖未在 matplotlib 中正確調整繪圖范圍

散點圖未在 matplotlib 中正確調整繪圖范圍

森欄 2022-01-18 17:41:33
我正在使用2.2.3 版plt.plot和plt.scatter在matplotlib2.2.3 版中繪制兩個高斯(一個以 0 為中心,另一個以 100 為中心)。出于任何原因,對于圖中第二條曲線的情況,子圖不會自動調整繪圖范圍scatter。當然我可以手動完成——在這個簡單的例子中——但實際上我擁有的是一個大網格,我不想一個一個地設置范圍。這是怎么回事?有什么辦法可以解決嗎?這是我的代碼: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.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()所以輸出是: 如果有人可以提供幫助,我會很高興,在此先感謝。任何答案或評論將不勝感激。
查看完整描述

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

http://img1.sycdn.imooc.com//61e68b6f0001f29f05830428.jpg

以上更多的是一個笑話,盡管它適用于這種特殊情況。更嚴肅的解決方案是創建實際數據的圖,然后直接將其刪除。這足以讓自動縮放對散點圖的數據范圍按預期工作。


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

http://img1.sycdn.imooc.com//61e68b7b000172b405890417.jpg

最后,考慮到在大網格的情況下,無論如何您當前都需要手動調整文本的位置。因此,真正的解決方案是創建一個為每個軸調用的函數,并讓它自動完成所有操作。


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

http://img1.sycdn.imooc.com//61e68b890001013409840581.jpg

查看完整回答
反對 回復 2022-01-18
?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

好吧,老實說:我不知道。我唯一能發現的是,所描述的問題似乎始于最大值低于 0.1 的圖。(即,簡單地嘗試plt.scatter(center1, hist1/10)plt.scatter(center2, hist2*10)

但是,從您的示例中,我并沒有真正得到scatter這里的需要。
如果您喜歡自動縮放plot和藍色圓圈 - 為什么不只是

plt.plot(center2, hist2, 'o')

...?


查看完整回答
反對 回復 2022-01-18
  • 2 回答
  • 0 關注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號