【问题标题】:How to connect the dots with a line?如何将点与线连接起来?
【发布时间】:2020-08-18 07:48:58
【问题描述】:

我能够从图像中绘制点。我现在正在尝试使用一条线连接这些点。本质上是在模仿那些连接点谜题。

这是我的代码:

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() 内部的内容,以便连接图像的各个点。

【问题讨论】:

  • 我正在努力解决plt.plot() 内部的内容,以便连接图像的各个点。你能更具体吗?

标签: python matplotlib python-imaging-library data-visualization scatter-plot


【解决方案1】:

您的第二个代码块中似乎有答案。 Matplotlib's plot function 可以使用 marker='o' 参数处理连接点的绘图,因此您根本不需要调用 scatter 。所以只要改变这一行:

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') 

【讨论】:

    猜你喜欢
    • 2015-08-04
    • 2021-08-30
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多