how to draw lines on plots in matplotlib and exit while loop

517
June 14, 2017, at 10:04 AM

I have a limited experience with programming. I am writing a small application in python using matplotlib which will allow me to digitise segments on a figure to be used to build polygons. I am using ginput() to add points on a graph and draw line but I need to implement a loop in order to have the possibility to draw a variable number of segments according to the feature observed. I am using Jupyter notebook. The applicationso far allows me to add the segments on the plot but I do not manage to exit the while loop. After some research, I saw the possibility to use keyboardinterrupt but it seems not to work. My code looks as follow:

t = np.arange(10)
plt.plot(t, np.sin(t)) # just some data on the plot
try:
    while True: # loop to allow me to add multiple lines and plot them 
        xy = plt.ginput(2, show_clicks = True) # set to two as I want just two points and the possibility to draw the line progressively
        x = [p[0] for p in xy]
        y = [p[1] for p in xy]
        line = plt.plot(x,y) #plot the line
        plt.draw()
        plt.show()
        print ('points: ', xy)
except KeyboardInterrupt: # my attempt to exit the while loop but in reality the figure just move to the background and a new figure is generated after 30 secondsf
    pass

thanks for the help and regards

Fabio

READ ALSO
find number of connected edges to a node and node with max connected edges

find number of connected edges to a node and node with max connected edges

In a graph, how do I find the number of connected (directly bound) edges to a node? And then, it would be trivial, but if there is any direct method to find the unique(s) node(s) with the maximum edges connected to them it would be niceI'm using Python...

334
How can I plot a style like gnuplot's 'with impulses' with matplotlib?

How can I plot a style like gnuplot's 'with impulses' with matplotlib?

I'd like to create a plot like the one below with python/pandas/matplotlibThe upper clip is no problem, but I haven't been able to get a plot like the lower clip to work

325
Should I have separate containers for Flask, uWSGI, and nginx?

Should I have separate containers for Flask, uWSGI, and nginx?

I intend to use Kubernetes and Ingress for load balancingI'm trying to learn how to set up Flask, uWSGI and Nginx

558
Creating a scatterplot from pandas dataframe after a groupby argument has been passed

Creating a scatterplot from pandas dataframe after a groupby argument has been passed

I would like to create a scatterplot for each category in the categorical variable 'Position' with 'last_speech' on the x-axis and 'mean_ARI' or 'mean_Flesch' or 'mean_Kincaid' on the y-axisI have searched through previous question/answers to no avail,...

305