Python ginput不允许绘制新点

时间:2014-06-02 19:40:26

标签: python matplotlib

此代码要求用户将三个点数字化(使用ginput),然后将这些点绘制到imshow图上方的屏幕上。它不是。有什么想法吗?

from pylab import show, ginput, rand, imshow, plot
from matplotlib.figure import Figure
import numpy as np

x1 = rand(103, 53) 
figure = Figure(figsize=(4, 4), dpi=100)
axes = figure.add_subplot(111)

imshow(x1)

# Get user input
x = ginput(3)
x = np.array(x)

# Plot the user's points to the screen
plot(x[:, 0], x[:, 1], 'k*', ms=50)
show()

2 个答案:

答案 0 :(得分:1)

不确定您尝试绘制的方式,星标和背景,反之亦然,但您需要更改通话顺序。

plot(10, 30, 'k*', ms=100)
x = ginput(2)
imshow(x1)

show()

当您点击两个点显示您的兰特数据时,这将显示一颗星。

这是使用从here获取的ginput的一个很好的例子:

import time
import numpy as np
import matplotlib.pyplot as plt

def tellme(s):
    print(s)
    plt.title(s,fontsize=16)
    plt.draw()

##################################################
# Define a triangle by clicking three points
##################################################
plt.clf()
plt.axis([-1.,1.,-1.,1.])
plt.setp(plt.gca(),autoscale_on=False)

tellme('You will define a triangle, click to begin')

plt.waitforbuttonpress()

happy = False
while not happy:
    pts = []
    while len(pts) < 3:
        tellme('Select 3 corners with mouse')
        pts = np.asarray( plt.ginput(3,timeout=-1) )
        if len(pts) < 3:
            tellme('Too few points, starting over')
            time.sleep(1) # Wait a second

    ph = plt.fill( pts[:,0], pts[:,1], 'r', lw=2 )

    tellme('Happy? Key click for yes, mouse click for no')

    happy = plt.waitforbuttonpress()

    # Get rid of fill
    if not happy:
        for p in ph: p.remove()

答案 1 :(得分:0)

您可以在对图像再次进行imshow之前完成。

import os, sys
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

im = np.array(Image.open(sys.argv[1]))
plt.imshow(im)

x = plt.ginput(1)

plt.close()
plt.imshow(im)

plt.plot(x[0][0], x[0][1], 'rs')

plt.show()