最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Why does matplotlib draw updates for the first interactive plot I show, but not the second? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to show two interactive plots. The second plot is displayed after the first plot is closed. In this minimal example, the interactive part just draws a dot wherever the user clicks and prints the coordinates to the console.

import matplotlib.pyplot as plt

def onclick(event):
    # This prints both times
    print(event.xdata, event.ydata)
    # This only works the first time
    plt.scatter(event.xdata, event.ydata)

for i in range(2):
    plt.plot([1, 2, 3, 4])
    plt.gca().figure.canvas.mpl_connect('button_press_event', onclick)
    with plt.ion():
        plt.show(block=True)

Using the code above, dots don't show up on the second plot. It still prints the coordinates, so the event is firing; it's just not drawing. Everything else works.

Things I've tried that didn't help:

  • Different backends (Qt, Tk)
  • plt.close() as the last step inside loop
  • plt.pause(1) as the last step inside loop
  • Using plt.ion() and plt.ioff() instead of the context manager
  • Manually detaching the event at the end of the loop, using canvas.mpl_disconnect(cid) with the cid returned from mpl_connect
  • plt.draw() & plt.pause(1) to try and force updates
  • Copying and pasting the same code twice instead of using a loop
  • Using fig, ax = plt.subplots() in the loop and ax.scatter() in the onclick handler to explicitly plot to the same axes

I'm using matplotlib 3.9.4 in Python 3.9.20 for compatibility with things beyond my control.

I'm trying to show two interactive plots. The second plot is displayed after the first plot is closed. In this minimal example, the interactive part just draws a dot wherever the user clicks and prints the coordinates to the console.

import matplotlib.pyplot as plt

def onclick(event):
    # This prints both times
    print(event.xdata, event.ydata)
    # This only works the first time
    plt.scatter(event.xdata, event.ydata)

for i in range(2):
    plt.plot([1, 2, 3, 4])
    plt.gca().figure.canvas.mpl_connect('button_press_event', onclick)
    with plt.ion():
        plt.show(block=True)

Using the code above, dots don't show up on the second plot. It still prints the coordinates, so the event is firing; it's just not drawing. Everything else works.

Things I've tried that didn't help:

  • Different backends (Qt, Tk)
  • plt.close() as the last step inside loop
  • plt.pause(1) as the last step inside loop
  • Using plt.ion() and plt.ioff() instead of the context manager
  • Manually detaching the event at the end of the loop, using canvas.mpl_disconnect(cid) with the cid returned from mpl_connect
  • plt.draw() & plt.pause(1) to try and force updates
  • Copying and pasting the same code twice instead of using a loop
  • Using fig, ax = plt.subplots() in the loop and ax.scatter() in the onclick handler to explicitly plot to the same axes

I'm using matplotlib 3.9.4 in Python 3.9.20 for compatibility with things beyond my control.

Share Improve this question edited 10 hours ago dirtbirb asked 11 hours ago dirtbirbdirtbirb 7110 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

With some debudding via the console I was able to make it work. Adding the same command at the end of the loop makes the dots appear in the second window.

import matplotlib.pyplot as plt

def onclick(event):
    # This prints both times
    print(event.xdata, event.ydata)
    # This only works the first time
    plt.gca().scatter(event.xdata, event.ydata)

for i in range(2):
    plt.plot([1, 2, 3, 4])
    plt.gca().figure.canvas.mpl_connect('button_press_event', onclick)
    with plt.ion():
        plt.show(block=True)
        plt.gca().figure.canvas.mpl_connect('button_press_event', onclick)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论