[英]Tensorboard not giving all the variables output for Visualization Python
[英]Python visualization of a function with 2 variables
我嘗試用2個變量來可視化一個簡單的函數。 當函數為x**2 + y**2
時,圖中的最小值位於預期的(0,0),但是當函數為(x + 1)**2 + (y + 1)**2
最小值位於(-1,1)。 x坐標正確,但y坐標不正確(應為(-1,-1))。 有人可以解釋為什么當我將x加上任何值時,最小值的x坐標總是正確的,但是當我將y加上任何數字時,最小值的y坐標總是位於錯誤的位置嗎? 我該如何解決?
import numpy as np
import matplotlib.pyplot as plt
# Define the function
def circle(x, y):
return (x + 1)**2+(y + 1)**2
# Grid for plotting
x = np.linspace(-3, 3, num = 101)
y = np.linspace(-3, 3, num = 101)
X, Y = np.meshgrid(x, y)
# 2D
plt.figure()
plt.imshow(circle(X, Y), extent = [-3, 3, -3, 3], cmap=plt.cm.jet)
plt.colorbar()
plt.show()
根據matplotlib文檔,您必須將origin
設置為“ lower”:
import numpy as np
import matplotlib.pyplot as plt
# Define the function
def circle(x, y):
return (x + 1) ** 2 + (y + 1) ** 2
# Grid for plotting
x = np.linspace(-3, 3, num = 101)
y = np.linspace(-3, 3, num = 101)
X, Y = np.meshgrid(x, y)
Z = circle(X, Y)
plt.imshow(Z, cmap = "jet", extent=[-3, 3, -3, 3], origin = "lower")
plt.colorbar()
plt.show()
如果未指定origin,則imshow
默認為標准值 ,在這種情況下為左上角。 因此,只有y值受到影響。
另外,您也可以使用pcolor
:
import numpy as np
import matplotlib.pyplot as plt
# Define the function
def circle(x, y):
return (x + 1) ** 2 + (y + 1) ** 2
# Grid for plotting
x = np.linspace(-3, 3, num = 101)
y = np.linspace(-3, 3, num = 101)
X, Y = np.meshgrid(x, y)
Z = circle(X, Y)
plt.pcolor(X, Y, Z, cmap = "jet")
plt.axis([-3, 3, -3, 3])
plt.colorbar()
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.