如何将用户在 Matplotlib 中输入的点传递给 np.array? [英] How do I pass on points that the user entered in Matplotlib to a np.array?

查看:55
本文介绍了如何将用户在 Matplotlib 中输入的点传递给 np.array?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个程序,允许用户首先在Matplotlib图中输入点,然后使用这些点创建Voronoi图.

I want to create a program that allows the user to first enter points in a Matplotlib plot and then creates a Voronoi Diagram using those points.

弄清楚了 2 个部分,但没有弄清楚连接.如何传递用户输入的点并在Voronoi部分中使用它?(我只需要知道如何将 points = np.random.rand(20,2)更改为用户输入的点.)

Figured out the 2 parts, but not the connection. How do I pass on the points that the user entered and use it in the Voronoi part? (I just need to know how to change the points = np.random.rand(20,2) to the points the user entered.)

绘制点:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])

def onclick(event):
    print('x=%d, y=%d, xdata=%f, ydata=%f' %
          (event.x, event.y, event.xdata, event.ydata))
    plt.plot(event.xdata, event.ydata, 'bo')
    fig.canvas.draw()

cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()`

创建Voronoi图:

Creating the Voronoi Diagram:

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi, voronoi_plot_2d

fig = plt.figure()

points = np.random.rand(20,2)

vor = Voronoi(points)

voronoi_plot_2d(vor)

plt.show()

推荐答案

您可以使用类来存储点,然后在同一图中绘制图表.在以下示例中,将用鼠标左键放置点,然后单击鼠标右键绘制voronoi图.如果您以后想向图中添加新点,您可以这样做.

You may use a class to store the points and later plot the diagram in that same plot. In the following example, you would place your points with the left mouse button and plot the voronoi diagram clicking the right mouse button. If you later want to add new points to the plot, you can do that.

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi, voronoi_plot_2d

fig, ax  = plt.subplots()
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])

class V():
    def __init__(self, ax = None):
        self.ax = ax
        if not self.ax: self.ax = plt.gca()
        tx = "left click to place points\nright click to plot voronoi diagram"
        self.text = self.ax.text(0.1,0.9, tx, transform=self.ax.transAxes, 
                                 ha="left", va="top")
        self.cid = fig.canvas.mpl_connect('button_press_event', self.onclick)
        self.points = []

    def onclick(self, event):
        self.text.set_visible(False)
        if event.button == 1:
            print('x=%d, y=%d, xdata=%f, ydata=%f' %
                  (event.x, event.y, event.xdata, event.ydata))
            plt.plot(event.xdata, event.ydata, 'bo')
            self.points.append((event.xdata, event.ydata))
        else:
            self.voronoi()
        fig.canvas.draw()

    def voronoi(self):
        self.ax.clear()
        vor = Voronoi(self.points)
        voronoi_plot_2d(vor, ax=self.ax)


v = V(ax=ax)
plt.show()

这篇关于如何将用户在 Matplotlib 中输入的点传递给 np.array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆