1

I have a function that plots a set of data with some points highlighted (the computer-measured values) and asks for user inputs about those points (the theoretical values for those points) in order to calibrate a device.

This used to work fine. Now, however, without changing anything, the plot doesn't show up until the end of the function. Here is an example of my code (I am running in jupyter notebook):

plot_peaks takes data and returns the x-coordinates of the peaks, as well as a plot of the data with the peaks highlighted.

%matplotlib qt
def func(data):
    plot_points = plot_peaks(data) # plot_peaks returns a list of points that gets saved as plot_points AND plots peaks in a window
    for number, point in enumerate(plot_points):
        print("What is the coordinate of point %f?" %(number)) # ask user for the theoretical coordinate of the recorded peak
        answer = input("> ")
        function_that_saves_user_inputs # placeholder function to save lines
    return user_inputs_vs_plot_points # dictionary

The user needs the plot to be able to see what the peaks are so that they can enter the theoretical value. However, the plot does not show up until all of the data points have been entered by the user.

If I use:

%matplotlib inline

then the plot shows up before the user enters anything. But I can't plot inline.

Things I've tried:

  • using ion() inside of plot_peaks (changes nothing)
  • plt.show(block=False) inside of plot_peaks (changes nothing)
  • adding another plt.show() inside of func() (pops up a blank window, when function terminates then the desired plot shows up in a new window)
  • changing the backend

1 Answer 1

3

For what it's worth, I found an answer to my specific problem (after lots of trial and error).

What I needed was for the function that draws a plot to pause for a long time before continuing. I don't know why, maybe it has to do with the plot not being finished before python starts executing the following commands. If anyone knows why that would be good to know.

All I did was add a plt.pause(1) at the end of the plot_peaks() method. This means the graph has time to fully display before the next part of the function is called.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.