对数刻度线已填充,看起来像半圆形

2024-05-23 19:13:58 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图用背景数据和拟合线创建一个半对数图时,拟合线看起来完全不稳定。你知道吗

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

出什么问题了?你知道吗


Tags: 数据importredshiftlenasnppltmass
1条回答
网友
1楼 · 发布于 2024-05-23 19:13:58

你看过用来绘制这些线的数组的内容了吗?我怀疑它们没有被分类,所以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

相关问题 更多 >