绘制 np.linspace 时,图形标签不是我想要的
When plotting a np.linspace the figure labeling is not what I wanted
我想为 julia 布景制作动画。不管怎样,到目前为止一切正常,
我只需要更改轴标签。在绘制我的值时
x 和 y 轴都显示 500x500 的线性空间。我比较喜欢看
[-1,1]x[-1,1] 区间我定义了 linspace。我该如何改变它?
谢谢:)
代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Parameters and Test-Function
f = lambda z: z ** 2 - 0.1+0.651*1j
N = 1000
R = 2
def pre_greyscale(f, c, N, R):
if np.abs(c) > R:
return 0
else:
for i in range(0, N):
c = f(c)
if np.abs(c) > R:
return i + 1
return N
# fig1 = plt.figure()
real = np.linspace(-1, 1, num=500)
imaginary = np.linspace(-1, 1, num=500)
pre_image = np.empty(shape=(real.size, imaginary.size))
for k, r in enumerate(real):
for p, i in enumerate(imaginary):
pre_image[p, k] = pre_greyscale(f, r + i * 1j, N, R)
def animate(m):
image = np.empty(shape=(real.size, imaginary.size))
for k in range(0, 500):
for p in range(0, 500):
if pre_image[p, k] <= m:
image[p, k] = 1 - pre_image[p, k] / m
# else:
# image[k, p] = 0
# mat = plt.imshow(image, cmap='gray')
# plt.show()
return image
imagelist = [animate(x) for x in range(N)]
fig = plt.figure() # make figure
# Initialize imshow
im = plt.imshow(imagelist[0], cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
# function to update figure
def updatefig(j):
# set the data in the axesimage object
im.set_array(imagelist[j])
# return the artists set
return [im]
# kick off the animation
ani = FuncAnimation(fig, updatefig, frames=N,
interval=20, blit=True)
ani.save('fractal2.gif', writer='pillow')
将 extent 参数添加到 plt.imshow
将设置正确的标签:
# Initialize imshow
im = plt.imshow(imagelist[0], cmap=plt.get_cmap('gray'), vmin=0, vmax=1, extent=[-1,1,-1,1])
我想为 julia 布景制作动画。不管怎样,到目前为止一切正常,
我只需要更改轴标签。在绘制我的值时
x 和 y 轴都显示 500x500 的线性空间。我比较喜欢看
[-1,1]x[-1,1] 区间我定义了 linspace。我该如何改变它?
谢谢:)
代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Parameters and Test-Function
f = lambda z: z ** 2 - 0.1+0.651*1j
N = 1000
R = 2
def pre_greyscale(f, c, N, R):
if np.abs(c) > R:
return 0
else:
for i in range(0, N):
c = f(c)
if np.abs(c) > R:
return i + 1
return N
# fig1 = plt.figure()
real = np.linspace(-1, 1, num=500)
imaginary = np.linspace(-1, 1, num=500)
pre_image = np.empty(shape=(real.size, imaginary.size))
for k, r in enumerate(real):
for p, i in enumerate(imaginary):
pre_image[p, k] = pre_greyscale(f, r + i * 1j, N, R)
def animate(m):
image = np.empty(shape=(real.size, imaginary.size))
for k in range(0, 500):
for p in range(0, 500):
if pre_image[p, k] <= m:
image[p, k] = 1 - pre_image[p, k] / m
# else:
# image[k, p] = 0
# mat = plt.imshow(image, cmap='gray')
# plt.show()
return image
imagelist = [animate(x) for x in range(N)]
fig = plt.figure() # make figure
# Initialize imshow
im = plt.imshow(imagelist[0], cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
# function to update figure
def updatefig(j):
# set the data in the axesimage object
im.set_array(imagelist[j])
# return the artists set
return [im]
# kick off the animation
ani = FuncAnimation(fig, updatefig, frames=N,
interval=20, blit=True)
ani.save('fractal2.gif', writer='pillow')
将 extent 参数添加到 plt.imshow
将设置正确的标签:
# Initialize imshow
im = plt.imshow(imagelist[0], cmap=plt.get_cmap('gray'), vmin=0, vmax=1, extent=[-1,1,-1,1])