在散点图(Python)中删除线下的数据
所以我有一段代码,可以绘制数据集的二维直方图。我是这样画的:在散点图(Python)中删除线下的数据,python,python-3.x,numpy,matplotlib,Python,Python 3.x,Numpy,Matplotlib,所以我有一段代码,可以绘制数据集的二维直方图。我是这样画的: histogram = plt.hist2d(fehsc, ofesc, bins=nbins, range=[[-1,.5],[0.225,0.4]]) 不过,我只想查看某一行以上的数据,因此我添加了以下内容,效果很好: counts = histogram[0] xpos = histogram[1] ypos = histogram[2] image = histogram[3] newcounts = counts #we'
histogram = plt.hist2d(fehsc, ofesc, bins=nbins, range=[[-1,.5],[0.225,0.4]])
不过,我只想查看某一行以上的数据,因此我添加了以下内容,效果很好:
counts = histogram[0]
xpos = histogram[1]
ypos = histogram[2]
image = histogram[3]
newcounts = counts #we're going to iterate over this
for i in range (nbins):
xin = xpos[i]
yin = ypos
yline = m*xin + b
reset = np.where(yin < yline) #anything less than yline we want to be 0
#index = index[0:len(index)-1]
countout = counts[i]
countout[reset] = 0
newcounts[i] = countout
我只想保留某行上面的数据:
xarr = np.arange(-1,0.5, 0.015)
yarr = m*xarr + b
plt.plot(xarr, yarr, color='r')
我尝试过使用变量的一些变体来运行循环,但我实际上不理解或不知道如何使其工作。您可以在绘图之前为数据定义一个
掩码,然后只绘制实际符合条件的数据点。下面是一个示例,其中某条直线上方的所有数据点均以绿色绘制,该直线下方的所有数据点均以黑色绘制
from matplotlib import pyplot as plt
import numpy as np
#the scatterplot data
xvals = np.random.rand(100)
yvals = np.random.rand(100)
#the line
b = 0.1
m = 1
x = np.linspace(0,1,num=100)
y = m*x+b
mask = yvals > m*xvals+b
plt.scatter(xvals[mask],yvals[mask],color='g')
plt.scatter(xvals[~mask],yvals[~mask],color='k')
plt.plot(x,y,'r')
plt.show()
结果是这样的
希望这有帮助
编辑:
如果要创建二维柱状图,其中线下方的部分设置为零,可以先使用numpy
(作为一个数组)生成柱状图,然后将该数组中的值设置为零(如果箱子位于线下方)。之后,可以使用plt.pcolormesh
绘制矩阵:
from matplotlib import pyplot as plt
import numpy as np
#the scatterplot data
xvals = np.random.rand(1000)
yvals = np.random.rand(1000)
histogram,xbins,ybins = np.histogram2d(xvals,yvals,bins=50)
#computing the bin centers from the bin edges:
xcenters = 0.5*(xbins[:-1]+xbins[1:])
ycenters = 0.5*(ybins[:-1]+ybins[1:])
#the line
b = 0.1
m = 1
x = np.linspace(0,1,num=100)
y = m*x+b
#hiding the part of the histogram below the line
xmesh,ymesh = np.meshgrid(xcenters,ycenters)
mask = m*xmesh+b > ymesh
histogram[mask] = 0
#making the plot
mat = plt.pcolormesh(xcenters,ycenters,histogram)
line = plt.plot(x,y,'r')
plt.xlim([0,1])
plt.ylim([0,1])
plt.show()
结果可能是这样的:您可以在打印之前为数据定义一个掩码,然后只打印实际符合条件的数据点。下面是一个示例,其中某条直线上方的所有数据点均以绿色绘制,该直线下方的所有数据点均以黑色绘制
from matplotlib import pyplot as plt
import numpy as np
#the scatterplot data
xvals = np.random.rand(100)
yvals = np.random.rand(100)
#the line
b = 0.1
m = 1
x = np.linspace(0,1,num=100)
y = m*x+b
mask = yvals > m*xvals+b
plt.scatter(xvals[mask],yvals[mask],color='g')
plt.scatter(xvals[~mask],yvals[~mask],color='k')
plt.plot(x,y,'r')
plt.show()
结果是这样的
希望这有帮助
编辑:
如果要创建二维柱状图,其中线下方的部分设置为零,可以先使用numpy
(作为一个数组)生成柱状图,然后将该数组中的值设置为零(如果箱子位于线下方)。之后,可以使用plt.pcolormesh
绘制矩阵:
from matplotlib import pyplot as plt
import numpy as np
#the scatterplot data
xvals = np.random.rand(1000)
yvals = np.random.rand(1000)
histogram,xbins,ybins = np.histogram2d(xvals,yvals,bins=50)
#computing the bin centers from the bin edges:
xcenters = 0.5*(xbins[:-1]+xbins[1:])
ycenters = 0.5*(ybins[:-1]+ybins[1:])
#the line
b = 0.1
m = 1
x = np.linspace(0,1,num=100)
y = m*x+b
#hiding the part of the histogram below the line
xmesh,ymesh = np.meshgrid(xcenters,ycenters)
mask = m*xmesh+b > ymesh
histogram[mask] = 0
#making the plot
mat = plt.pcolormesh(xcenters,ycenters,histogram)
line = plt.plot(x,y,'r')
plt.xlim([0,1])
plt.ylim([0,1])
plt.show()
结果是这样的:那么我是否正确理解了这一点,你想要一些数据的散点图和一条数据线,然后你想要删除线下的所有点?或者可能是另一种颜色?你好!您的第一个解释是正确的-我希望删除线下的所有数据,因为我想对线上的数据进行进一步分析。那么我是否正确理解了这一点,您希望一些数据的散点图和一条贯穿数据的线,然后您希望删除线下的所有点?或者可能是另一种颜色?你好!你的第一个解释是正确的-我想删除线下的所有数据,因为我想对线上的数据做进一步的分析。帮了大忙!我已经有了2d柱状图,我只需要让散点图开始工作。:)帮了不少忙!我已经有了2d柱状图,我只需要让散点图开始工作。:)