matplotlib交互式图形绘制(手动在图形上绘制线条) [英] matplotlib interactive graphing (manually drawing lines on a graph)

查看:74
本文介绍了matplotlib交互式图形绘制(手动在图形上绘制线条)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功使用matplotlib绘制了一组按日期排序的数据(X轴为日期).但是,我希望能够在绘制的图形上手动从一个 (date1, y1) 到另一个 (date2, y2) 绘制线条.

I have succesfully plotted a set of date sequenced data (X axis is date) using matplotlib. However, I want to be able to manually draw lines from one (date1, y1) to another (date2, y2) on the plotted graph.

我似乎找不到任何示例来说明如何做到这一点 - 或者甚至是可能的.

I can't seem to find any examples that show how to do this - or indeed if it is even posible.

总而言之,这就是我想要做的:

To summarize, this is what I want to do:

  1. 在绘制的图形上绘制一组线
  2. 将手动绘制的线数据保存到文件
  3. 从文件中加载手动绘制的线数据(以重新创建图形)
  4. 理想情况下,我想存储有关绘制线条(例如颜色,线条宽度等)的元数据"

有人可以发布一个骨架片段(最好带有指向更多信息的链接),以展示我如何开始实现这一点(主要要求是能够在图形上手动绘制线条然后保存/加载线条成情节).

Can someone post a skeleton snippet (preferably with links to further info), to show how I may get started with implementing this (the main requirements being the ability to manually draw lines on a graph and then to save/load the lines into a plot).

注意:手动"是指能够通过单击一个点,然后单击绘制的图形中的另一个点来绘制线条.在两点之间画一条线(或者简单地点击一个点并在绘制的图形上的另一个点拖动并释放鼠标)

Note: By 'manually', I mean to be able to draw the lines by clicking on a point, and then clicking on another point in the plotted graph. to draw a line between the two points (or simply clicking on a point and dragging and releasing the mouse at another point on the plotted graph)

[[更新]]

dawe,非常感谢您提供的片段.这使我可以做我想做的事情-但是,一旦在画布上绘制了线条(第二次单击鼠标后),GUI就会崩溃,并且我会在控制台上收到以下警告消息:

dawe, thanks very much for the snippet you provided. This allows me to do what I am trying to do - however, as soon as the line is drawn on the canvas (after the second mouse click), the GUI crashes and I get this warning message on the console:

/usr/local/lib/python2.6/dist-packages/matplotlib/backend_bases.py:2192: DeprecationWarning: Using default event loop until function specific to this GUI is implemented
  warnings.warn(str,DeprecationWarning)

您知道造成此警告和程序突然终止的原因吗?

Do you know what is causing this warning and the abrupt program termination?

另外,是否可以在图形上绘制多条线?(我猜这将涉及编写某种事件处理程序来实例化一个 linedrawer 变量).目前,我有机会在应用程序"突然终止之前只画一条线.

Also, is it possible to draw more than one line on the graph? (I'm guessing this will involve writing some kind of event handler that will instantiate a linedrawer variable). At the moment , I get the chance to draw only one line before the 'app' abruptly terminates.

推荐答案

我会这样写:

import matplotlib.pyplot as plt
class LineDrawer(object):
    lines = []
    def draw_line(self):
        ax = plt.gca()
        xy = plt.ginput(2)

        x = [p[0] for p in xy]
        y = [p[1] for p in xy]
        line = plt.plot(x,y)
        ax.figure.canvas.draw()

        self.lines.append(line)

使用 ginput()可以避免更复杂的事件处理.它起作用"的方式是您绘制一些东西:

Using ginput() you can avoid more complicated event handling. The way it 'works' is you plot something:

plt.plot([1,2,3,4,5])
ld = LineDrawer()
ld.draw_line() # here you click on the plot

为了将行数据保存/加载到文件中,您可以使用 pickle shelve 轻松实现一种方法.您还可以通过方法 draw_line()

For saving/loading the line data to a file you can easily implement a method using pickle or shelve. You can also pass the necessary metadata by the method draw_line()

这篇关于matplotlib交互式图形绘制(手动在图形上绘制线条)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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