![](/img/trans.png)
[英]Pandas: Filling missing values, based on particular column, by adding some days
[英]Filling pixels under or above some function
似乎是一個簡單的問題,但我無法解決它。
我有一個配置文件,我在其中聲明了一些函數。 它看起來像這樣:
"bandDefinitions" : [
{
"0": ["x^2 + 2*x + 5 - y", "ABOVE"]
},
{
"0": ["sin(6*x) - y", "UNDER"]
},
{
"0": ["tan(x) - y", "ABOVE"]
}
]
這些函數應該生成 3 張圖像。 每個圖像都應根據方程的解進行填充,並提供 position(低於或高於)。 我需要將坐標系移動到圖像的中心,所以我將-y
添加到等式中。 應填充的圖像部分應為白色,而另一部分應為黑色。
為了解釋我的意思,我提供了二次函數和 sin 函數的圖像。
我正在做的是求解x in [-W/2, W/2]
的方程並將解存儲到數組中,如下所示:
#Generates X axis dots and solves an expression which defines a band
#Coordinate system is moved to the center of the image
def __solveKernelDefinition(self, f):
xAxis = range(-kernelSize, kernelSize)
dots = []
for x in xAxis:
sol = f(x, kernelSize/2)
dots.append(sol)
print(dots)
return dots
我正在測試某些像素是否應該像這樣塗成白色:
def shouldPixelGetNoise(y, x, i, currentBand):
shouldGetNoise = True
for bandKey in currentBand.bandDefinition.keys():
if shouldGetNoise:
pixelSol = currentBand.bandDefinition[bandKey][2](x, y)
renderPos = currentBand.bandDefinition[bandKey][1]
bandSol = currentBand.bandDefinition[bandKey][0]
shouldGetNoise = shouldGetNoise and pixelSol <= bandSol[i] if renderPos == Position.UNDER else pixelSol >= bandSol[i]
else:
break
return shouldGetNoise
def kernelNoise(kernelSize, num_octaves, persistence, currentBand, dimensions=2):
simplex = SimplexNoise(num_octaves, persistence, dimensions)
data = []
for i in range(kernelSize):
data.append([])
i1 = i - int(kernelSize / 2)
for j in range(kernelSize):
j1 = j - int(kernelSize / 2)
if(shouldPixelGetNoise(i1, j1, i, currentBand)):
noise = normalize(simplex.fractal(i, j, hgrid=kernelSize))
data[i].append(noise * 255)
else:
data[i].append(0)
對於凸二次函數,我只會得到很好的 output 。 如果我嘗試將它們結合起來,我會得到一個黑色的圖像。 Sin
根本不起作用。 我看到這種蠻力方法不會把我帶到任何地方,所以我想知道我應該使用什么算法來生成這些類型的圖像?
據我了解,您想要 plot 您的功能並填寫這些功能的上方或下方。 您可以通過在numpy
中創建一個網格(即二維笛卡爾坐標系)並在網格上定義您的函數來輕松地做到這一點。
import numpy as np
import matplotlib.pyplot as plt
max_ax = 100
resolution_x = max_ax/5
resolution_y = max_ax/20
y,x = np.ogrid[-max_ax:max_ax+1, -max_ax:max_ax+1]
y,x = y/resolution_y, x/resolution_x
func1 = x**2 + 2*x + 5 <= -y
resolution_x = max_ax
resolution_y = max_ax
y,x = np.ogrid[-max_ax:max_ax+1, -max_ax:max_ax+1]
y,x = y/resolution_y, x/resolution_x
func2 = np.sin(6*x) <= y
func3 = np.tan(x) <= -y
fig,ax = plt.subplots(1,3)
ax[0].set_title('f(x)=x**2 + 2*x + 5')
ax[0].imshow(func1,cmap='gray')
ax[1].set_title('f(x)=sin(6*x)')
ax[1].imshow(func2,cmap='gray')
ax[2].set_title('f(x)=tan(x)')
ax[2].imshow(func3,cmap='gray')
plt.show()
這是你想要的?
編輯:我調整了 x 軸和 y 軸的限制。 因為,例如, sin(x)
在 [-1,1] 范圍之外沒有多大意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.