如何用线连接点?
How to connect the dots with a line?
我能够从图像中绘制点。我现在正在尝试使用一条线连接点。本质上是模仿那些连点拼图。
这是我的代码:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
original_image = Image.open("jg.jpg")
bw_image = original_image.convert('1')
bw_image_array = np.array(bw_image, dtype=np.int)
black_indices = np.argwhere(bw_image_array == 0)
chosen_black_indices = black_indices[np.random.choice(black_indices.shape[0], replace=False, size=90000)]
plt.figure(figsize=(5, 5), dpi=100)
plt.scatter([x[1] for x in chosen_black_indices], [x[0] for x in chosen_black_indices], color='black', s=1)
plt.gca().invert_yaxis()
plt.xticks([])
plt.yticks([])
plt.show()
我的目标是:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.random_sample(size=100)
y = np.random.random_sample(size=100)
fig, ax = plt.subplots()
ax.scatter(x,y)
plt.plot(x, y, '-o')
plt.show()
为了连接图像的各个点,我正在努力研究 plt.plot()
内部的内容。
您的第二个代码块中似乎有答案。 Matplotlib's plot function 可以使用 marker='o'
参数处理绘制连接点,因此您根本不需要调用散点图。所以只需更改此行:
plt.scatter([x[1] for x in chosen_black_indices],
[x[0] for x in chosen_black_indices],
color='black', s=1)
对此:
plt.plot([x[1] for x in chosen_black_indices],
[x[0] for x in chosen_black_indices],
marker='o',
color='black')
我能够从图像中绘制点。我现在正在尝试使用一条线连接点。本质上是模仿那些连点拼图。
这是我的代码:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
original_image = Image.open("jg.jpg")
bw_image = original_image.convert('1')
bw_image_array = np.array(bw_image, dtype=np.int)
black_indices = np.argwhere(bw_image_array == 0)
chosen_black_indices = black_indices[np.random.choice(black_indices.shape[0], replace=False, size=90000)]
plt.figure(figsize=(5, 5), dpi=100)
plt.scatter([x[1] for x in chosen_black_indices], [x[0] for x in chosen_black_indices], color='black', s=1)
plt.gca().invert_yaxis()
plt.xticks([])
plt.yticks([])
plt.show()
我的目标是:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.random_sample(size=100)
y = np.random.random_sample(size=100)
fig, ax = plt.subplots()
ax.scatter(x,y)
plt.plot(x, y, '-o')
plt.show()
为了连接图像的各个点,我正在努力研究 plt.plot()
内部的内容。
您的第二个代码块中似乎有答案。 Matplotlib's plot function 可以使用 marker='o'
参数处理绘制连接点,因此您根本不需要调用散点图。所以只需更改此行:
plt.scatter([x[1] for x in chosen_black_indices],
[x[0] for x in chosen_black_indices],
color='black', s=1)
对此:
plt.plot([x[1] for x in chosen_black_indices],
[x[0] for x in chosen_black_indices],
marker='o',
color='black')