gpt4 book ai didi

python - 对数刻度线被填充,看起来像一个半圆

转载 作者:太空宇宙 更新时间:2023-11-04 00:51:59 25 4
gpt4 key购买 nike

当我尝试创建带有背景数据和拟合线的半对数图时,拟合线最终看起来完全不稳定。

import numpy as np
import matplotlib.pyplot as plt

k=0

for i in np.arange(0,len(emceeredshifts),7):
zbin=emceeredshifts[i+1]
lowradius = radius[(redshift <= (zbin + halfwidth)) & (redshift >= (zbin - halfwidth)) & (radius > 1) & (radius<20) &(mass>10.5)].flatten()
lowmass = mass[(redshift <= (zbin + halfwidth)) & (redshift >= (zbin - halfwidth)) & (radius > 1) & (radius<20)&(mass>10.5)].flatten()
if len(lowradius)>0:
lowfit = np.polyfit(lowmass, lowradius, 1)
lowlin,lowinter=np.poly1d(lowfit)
lowbestfit = lowinter + lowlin * (lowmass )
plt.plot( lowmass, lowbestfit, color=rainbowcolors[k], label=str(zbin))
plt.scatter( lowmass, lowradius, color=rainbowcolors[k], marker='.', alpha=.2, edgecolor='none')
k+=1

plt.legend(loc='lower right')

plt.title("Galaxy radius vs mass\nlinear mcmc mass predictions")
plt.xlabel("Log $M_\odot$")
plt.ylabel("Physical radius (kpc)")
plt.ylim(2,15)
plt.xlim(10.6,11.8)

plt.yscale('log')
plt.show()

这是有问题的半对数结果。 Buggy log-scale matplotlib line plot

这是我移除对数刻度后得到的结果。当 y 轴是线性时,线看起来像线,数据看起来像数据。 Linear scale plot

出了什么问题?

最佳答案

您是否看过用于绘制这些线条的数组的内容?我怀疑它们没有排序,所以 mpl 在点之间来回画线。在线性空间中,这相当于沿着同一条线绘制,因此您看不到它,但在对数空间中,由于曲线,它很明显。

我认为这个最小的例子说明了问题:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(1000)
y = x.copy()

fig,(ax1,ax2,ax3) = plt.subplots(3,figsize=(7,7))
fig.subplots_adjust(hspace=0.3)

ax1.plot(x,y)
ax1.set_title('linear')

ax2.plot(x,y)
ax2.set_yscale('log')
ax2.set_title('log, unsorted')

ind=np.argsort(x)
ax3.plot(x[ind],y[ind])
ax3.set_yscale('log')
ax3.set_title('log, sorted')

plt.show()

enter image description here

关于python - 对数刻度线被填充,看起来像一个半圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36734578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com