如何在散点图上分隔数据集

时间:2014-07-09 21:10:54

标签: python matplotlib astronomy

我对python很新,但我有兴趣学习一种新技术,根据它们落在散点图中的位置,我可以使用不同的标记在散点图中识别不同的数据点。

我的具体例子很多:http://www.astroml.org/examples/datasets/plot_sdss_line_ratios.html

我有一个BPT图,想要沿着分界线分割数据。

我有这种格式的数据集:

data = [[a,b,c],
        [a,b,c],
        [a,b,c]
]

我的分界线也有以下内容:

NII   = np.linspace(-3.0, 0.35)

def log_OIII_Hb_NII(log_NII_Ha, eps=0):
    return 1.19 + eps + 0.61 / (log_NII_Ha - eps - 0.47)

任何帮助都会很棒!

2 个答案:

答案 0 :(得分:1)

评论部分没有足够的空间。与@DrV写的不太相似,但可能更符合天文学的倾向:

import random
import numpy as np
import matplotlib.pyplot as plt

def log_OIII_Hb_NII(log_NII_Ha, eps=0):
    return 1.19 + eps + 0.61 / (log_NII_Ha - eps - 0.47)

# Make some fake measured NII_Ha data
iternum = 100

# Ranged -2.1 to 0.4:
Measured_NII_Ha = np.array([random.random()*2.5-2.1 for i in range(iternum)])
# Ranged -1.5 to 1.5:
Measured_OIII_Hb = np.array([random.random()*3-1.5 for i in range(iternum)])

# For our measured x-value, what is our cut-off value
Measured_Predicted_OIII_Hb = log_OIII_Hb_NII(Measured_NII_Ha)

# Now compare the cut-off line to the measured emission line fluxes
# by using numpy True/False arrays
#
# i.e., x = numpy.array([1,2,3,4])
# >> index = x >= 3
# >> print(index)
# >> numpy.array([False, False, True, True])
# >> print(x[index])
# >> numpy.array([3,4])

Above_Predicted_Red_Index = Measured_OIII_Hb > Measured_Predicted_OIII_Hb
Below_Predicted_Blue_Index = Measured_OIII_Hb < Measured_Predicted_OIII_Hb
# Alternatively, you can invert Above_Predicted_Red_Index



# Make the cut-off line for a range of values for plotting it as
# a continuous line
Predicted_NII_Ha = np.linspace(-3.0, 0.35)
Predicted_log_OIII_Hb_NII = log_OIII_Hb_NII(Predicted_NII_Ha)

fig = plt.figure(0)
ax = fig.add_subplot(111)

# Plot the modelled cut-off line
ax.plot(Predicted_NII_Ha, Predicted_log_OIII_Hb_NII, color="black", lw=2)

# Plot the data for a given colour
ax.errorbar(Measured_NII_Ha[Above_Predicted_Red_Index], Measured_OIII_Hb[Above_Predicted_Red_Index], fmt="o", color="red")
ax.errorbar(Measured_NII_Ha[Below_Predicted_Blue_Index], Measured_OIII_Hb[Below_Predicted_Blue_Index], fmt="o", color="blue")

# Make it aesthetically pleasing
ax.set_ylabel(r"$\rm \log([OIII]/H\beta)$")
ax.set_xlabel(r"$\rm \log([NII]/H\alpha)$")

plt.show()

plot of example code

答案 1 :(得分:-1)

我假设您的示例中的像素坐标为a, b。然后,具有c s的列用于计算点是否属于两个组之一。

首先将您的数据设为ndarray

import numpy as np

data = np.array(data)

现在,您可以通过检查数据的哪一部分属于哪个区域来创建两个数组:

dataselector = log_OIII_Hb_NII(data[:,2]) > 0

这会创建一个Trues和Falses向量,只要第三列(第2列)中的数据从函数中给出正值,它就会为True。向量的长度等于data中的行数。

然后你可以绘制两个数据集:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

# the plotting part
ax.plot(data[dataselector,0], data[dataselector,1], 'ro')
ax.plot(data[-dataselector,0], data[-dataselector,1], 'bo')

即:

  • 创建一个True / False值列表,告知data哪些行属于哪个组
  • 绘制两个组(-dataselector表示“dataselector中存在假的​​所有行”