Python:绘制负(x,y)坐标的频率
Python: Plot frequency of negative (x,y) coordinates
我正在尝试绘制从 (0,0) 开始的二维整数随机游走的频率。首先,我得到一个 (x,y) 坐标列表,其中一些是负坐标,我在正确绘制它们时遇到了问题。我知道我目前的尝试是错误的,因为 (0,0) 有时看起来根本不在随机游走中。
这是我的尝试:
想法是转置所有 (x,y) 坐标,使它们位于第一象限,然后操纵 extent
上的轴将它们转置回原始形式。我这样做是为了避免在 grid
的构造中进行不需要的列表操作,这会使情节 'jump'
# generate random walk
import random
def random_walk():
a = 0
b = 0
while True:
yield (a, b)
i = random.choice([0, 1])
if i == 0:
a = random.choice([a-1, a+1])
else:
b = random.choice([b-1, b+1])
以及密谋自己的企图:
import matplotlib.pyplot as plt
import itertools
import numpy as np
# generate random walk of length 1000
walk = list(itertools.islice(random_walk(), 0, 1000))
xy = list(zip(*walk))
x = list(xy[0])
y = list(xy[1])
ext = (min(x), max(x), min(y), max(y))
min_x = min(x)
min_y = min(y)
# transpose all x.- and y.-coordinates:
for i in range(len(x)):
x[i] -= min_x
for i in range(len(y)):
y[i] -= min_y
walk_new = list(zip(x,y))
grid = np.zeros((max(x) + 1, max(y) + 1))
# Put frequencies in grid:
for a,b in walk_new:
grid[a][b] += 1
plt.imshow(grid)
plt.colorbar()
我注意到当我 运行 你的代码时,原点 (0,0) 没有按预期显示在你的热图上。我保留了你的 random_walk 函数,但我认为在这里使用 matplotlib 的 hist2d 会更容易,因为 (x,y) 坐标完全按照你喜欢的方式显示,不需要 t运行 设置你的坐标生成。
import matplotlib.pyplot as plt
import itertools
import numpy as np
# generate random walk of length 1000
walk = list(itertools.islice(random_walk(), 0, 1000))
xy = list(zip(*walk))
x = list(xy[0])
y = list(xy[1])
heatmap, xedges, yedges = np.histogram2d(x, y, bins=[np.arange(min(x),max(x),1),np.arange(min(y),max(y),1)])
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
plt.imshow(heatmap, origin='lower', extent=extent)
# set bins to be 1 unit apart since your (x,y) coordinates are integers
# plt.hist2d(x,y, bins=[np.arange(min(x),max(x),1),np.arange(min(y),max(y),1)])
plt.colorbar()
plt.show()
我正在尝试绘制从 (0,0) 开始的二维整数随机游走的频率。首先,我得到一个 (x,y) 坐标列表,其中一些是负坐标,我在正确绘制它们时遇到了问题。我知道我目前的尝试是错误的,因为 (0,0) 有时看起来根本不在随机游走中。
这是我的尝试:
想法是转置所有 (x,y) 坐标,使它们位于第一象限,然后操纵 extent
上的轴将它们转置回原始形式。我这样做是为了避免在 grid
的构造中进行不需要的列表操作,这会使情节 'jump'
# generate random walk
import random
def random_walk():
a = 0
b = 0
while True:
yield (a, b)
i = random.choice([0, 1])
if i == 0:
a = random.choice([a-1, a+1])
else:
b = random.choice([b-1, b+1])
以及密谋自己的企图:
import matplotlib.pyplot as plt
import itertools
import numpy as np
# generate random walk of length 1000
walk = list(itertools.islice(random_walk(), 0, 1000))
xy = list(zip(*walk))
x = list(xy[0])
y = list(xy[1])
ext = (min(x), max(x), min(y), max(y))
min_x = min(x)
min_y = min(y)
# transpose all x.- and y.-coordinates:
for i in range(len(x)):
x[i] -= min_x
for i in range(len(y)):
y[i] -= min_y
walk_new = list(zip(x,y))
grid = np.zeros((max(x) + 1, max(y) + 1))
# Put frequencies in grid:
for a,b in walk_new:
grid[a][b] += 1
plt.imshow(grid)
plt.colorbar()
我注意到当我 运行 你的代码时,原点 (0,0) 没有按预期显示在你的热图上。我保留了你的 random_walk 函数,但我认为在这里使用 matplotlib 的 hist2d 会更容易,因为 (x,y) 坐标完全按照你喜欢的方式显示,不需要 t运行 设置你的坐标生成。
import matplotlib.pyplot as plt
import itertools
import numpy as np
# generate random walk of length 1000
walk = list(itertools.islice(random_walk(), 0, 1000))
xy = list(zip(*walk))
x = list(xy[0])
y = list(xy[1])
heatmap, xedges, yedges = np.histogram2d(x, y, bins=[np.arange(min(x),max(x),1),np.arange(min(y),max(y),1)])
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
plt.imshow(heatmap, origin='lower', extent=extent)
# set bins to be 1 unit apart since your (x,y) coordinates are integers
# plt.hist2d(x,y, bins=[np.arange(min(x),max(x),1),np.arange(min(y),max(y),1)])
plt.colorbar()
plt.show()