Store Click Coordinates Using Matplotlib - python-3.x

I am trying to display an image, click on it somewhere, then store those coordinates into a variable. However, I am unable to do so. I can print the click coordinates no problem, but I cannot figure out a way to actually store those coordinates. The matplotlib documentation has some tutorials on how to use "fig.canvas.mpl_connect" in general, but none of the routines cover storing the click coordinates, which is what I want to do. There are some tutorials on StackExchange, as well as other websites, but they seem to be for outdated versions of python and/or matplotlib.
Here is my simple code right now:
import matplotlib.pyplot as plt
x = 0
def onclick(event):
print(event.xdata)
print(event.ydata)
global x
x = event.xdata
fig, ax = plt.subplots(figsize=(8,8))
plt.show()
cid = fig.canvas.mpl_connect('button_press_event', onclick)
print(x)
Upon running this code, it immediately prints '0', THEN displays the image. When I then click on that figure, I get coordinates printed to the console. I have tried putting a pause command before the print statement, but it just waits to print '0', then displays the image. Essentially, I need it to display the image so I can click it, THEN print the coordinates of my click.
Any help would be appreciated. I am also open to another method of obtaining the click coordinates, if one exists. Thank you.

Related

Matplotlib Text artist - how to get size? (not using pyplot)

Background
I've moved some code to use matplotlib only, instead of pyplot (The reason is it's generating png files in multi-process with futures, and pyplot isn't thread/process safe that way).
So I'm creating my figure and axis via matplotlib.Figure(), not via pyplot.
I've got some code that draw's a text 'table' on a figure, with one side right justified and the other left. In order to keep the spacing between the two sides constant, I was previously using get_window_extent() on my left-side text artist to figure out the location of the right hand side:
# draw left text at figure normalised coordinates, right justified
txt1 = figure.text(x, y, left_str,
ha='right', color=left_color, fontsize=fontsize)
# get the bounding box of that text - this is in display coords
bbox = txt1.get_window_extent(renderer=figure.canvas.get_renderer())
# get x location for right hand side offset from left's bbox
trans = figure.transFigure.inverted()
xr, _ = trans.transform((bbox.x1 + spacing, bbox.y0))
# draw right side text, using lhs's y value
figure.text(xr, y, right_str,
ha='left', color=right_color, fontsize=fontsize)
Problem
My problem is now that I'm not using pyplot, the above code fails to work, because figure.canvas.get_renderer() fails to return a renderer, as I haven't set one, and am simply using Figure.savefig(path) to save my file when I'm done.
So is there a way to find out the bounding box size of an Artist in a Figure without having a renderer set?
From looking at legend, which allows you to use a bounding box line with variable text size, I'm presuming there is, but I can't find how.
I've had a look at https://matplotlib.org/3.1.3/tutorials/intermediate/artists.html, and also tried matplotlib.artist.getp(txt1) on the above, but didn't find any seemingly helpful properties.

Python: PyAutoGui click location is off by a few pixels when using an image to locate

Goal of the program: open a web browser tab to youtube, use a saved image of the "Youtube" button on the Youtube home screen to move the mouse position to that location, do a mouse click when there
Issue: The mouse moves to a location that is off by a few pixels (-29 x, -35 y) when performing the click() step. The coordinates are correct at the time of locateCenterOnScreen but are different when it does click()
What I've tried: I had the program print out the coordinates of the picture when it takes it's location and at that point in time the coordinates are correct, I used a mouse position program to narrow down how much its off by.
My Question: What is causing the position of the click() to be offset by these few pixels and how do I fix it?
import pyautogui as auto
import webbrowser
import time
site = "https://www.youtube.com/"
webbrowser.open_new_tab(site)
time.sleep(5)
x, y = auto.locateCenterOnScreen('test.png')
print(x)
print(y)
try:
auto.click(x,y)
except:
print("Not Found")
I ended up re-taking the picture I used for the program to locate and it works now. I'm unsure why the original one did not work as intended though.
probably your window was resized so the widht and height of the image you where looking for also changed.
I recommend using:
win = pygetwindow.getWindowsWithTitle('windownname')[0]
win.size = (1600, 900)
to resize a window

Is it possible to update inline plots in Python (Spyder)?

Setup: Anaconda 3 (Win10 64), Spyder 4 and Python 3.7. The IPython Graphics setting is default (Inline).I'm still a new to Python but I've looked around and have not found an answer that solves my problem so far. Thanks everyone in advance.
So in this setup, whenever I create a plot using matplotlib, it appears in the plot pane of Spyder. e.g.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.DataFrame(np.random.randint(0,100,size=(100, 1)), columns=list('A'))
bp = df.boxplot(column = 'A')
creates a boxplot. Now, if I want to add a title to the plot, the code would be
bp.set_title("This Title")
This is where I'm getting some problems. If I run the entire block together
df = pd.DataFrame(np.random.randint(0,100,size=(100, 1)), columns=list('A'))
bp = df.boxplot(column = 'A')
bp.set_title("This Title")
then I get a box plot with "This Title" as the title, showing up in the plot pane,
which is what I want.
However, if I run the above code line by line in the IPython console, the 2nd line will produce a boxplot as expected, but the 3rd line will not have an effect on the image in the plot pane, so the image in the plot pane still do not have a title
Now,if i go to Tools > Preference >IPython Console > Graphics and set the graphics backend to Automatic instead of the default Inline, then when I run the code in the Console line by line, I get an image that pops up in another window, and that it does update/refreshes based on new lines entered into the console. I understand that the inline plots are supposed to be static, but I thought I saw another post where someone said that it is possible to update inline plots? So now my questions are:
Do plots only update/refresh by line codes in the IPython console if the Graphics Backend is not static like inline?
Why do I get different result when I run code blocks vs line by line?
If it is possible to update the inline plots (preferably in the plot pane of Spyder), how do you do it? I've tried various methods to redraw the plots,for example
plt.show()
plt.draw()
bp.get_figure().canvas.draw()
but none of these updates the image in the plot pane. I figured that even if I can't update the image, I should at least be able to redraw it (i.e a 2nd image appears in the plot pane with the update characteristics). But nothing I've tried worked so far. Please advise and thanks again.
(Spyder maintainer here) About your questions:
Do plots only update/refresh by line codes in the IPython console if the Graphics Backend is not static like inline?
Correct.
Why do I get different result when I run code blocks vs line by line?
Because when you run code cells (which is what I think you mean by "code blocks") your plot is shown at the end of that code and hence it takes all modifications you've done to it in intermediate lines.
If it is possible to update the inline plots (preferably in the plot pane of Spyder), how do you do it?
No, it's not possible. As you correctly mentioned above, inline plots are static images, so they can't be modified.

Python - Spyder ignoring picker enabled plot

I am writing this script in Spyder (Python 3.5) and I want it to do this:
1) Plot something
2) Allow me to pick some values from the plot
3) Store those values into a variable
4) Do something with that variable
I have checked this thread: Store mouse click event coordinates with matplotlib and modified the function presented there for my own code. The problem I have is that spyder seems to ignore the interactive plot and runs the whole script at once, without waiting for me to pick any values from the plot. As I am using the values for further calculations, I obviously get an error from this. I have even tried to set an input('Press enter to continue...') after the plot, to see if it made it stop and wait for my pickings, but it does not work either.
When I run the script step by step, it works fine, I get the plot, pick my values, print the variable and find all of them in there and use them afterwards. So the question is: how can I make it work when I run the whole script?
Here is my code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import plot as plot
def onpick(event):
ymouse = event.ydata
print ('y of mouse: {:.2f}'.format(ymouse))
times.append(ymouse)
if len(times)==5:
f.canvas.mpl_disconnect(cid)
return times
#
t=np.arange(1000)
y=t**3
f=plt.figure(1)
ax=plt.gca()
ax.plot(t,y,picker=5)
times=[]
cid=f.canvas.mpl_connect('button_press_event',onpick)
plt.show()
#Now do something with times
mtimes=np.mean(times)
print(mtimes)
(Spyder maintainer here) I think to solve this problem you need to go to
Preferences > IPython console > Graphics
and turn off the option called Activate support. That will make your script to block the console when a plot is run, so you can capture the mouse clicks you need on it.
The only problem is you need to run
In [1]: %matplotlib qt5
before starting to run your code because Spyder doesn't that for you anymore.

Matplotlib Crash When Figure 1 not Closed Last

I am plotting mutliple figures using Matplotlib using Python 3.4.
When the multiple figures are open and I close the windows closing the first figure last (ie once all other figures are closed) python does not crash.
If, however, I close the first figure that was plotted first and then close the rest Python crashes.
It seems as though you need to close the windows in such an order that the first window that was opened must be closed last. Has anyone else experienced and is there a solution?
Here is a trivial example code that can be used to verify:
import matplotlib.pyplot as plt
plt.figure(1) # the first figure
plt.plot([1,2,3])
plt.figure(2) # a second figure
plt.plot([4,5,6])
plt.show()
As discussed on the IPython bug tracker this is a bug in a TCL/TK library which is shipped with python 3.4 on windows.
Changing the backend to Qt works around the problem by using a different gui framework.
The way I have managed to solve this issue has been to use Qt4 as the matplotlib backend.
Simply add the following two lines of code after importing matplotlib.
import matplotlib as mpl
mpl.rcParams['backend'] = "qt4agg"
mpl.rcParams['backend.qt4'] = "PySide"
This is what I do on Python 3 and have no plot closing errors
I use this to do effectively what should be done by plt.close('all'):
def closeall(): # this closes all figures in reverse order
l = plt.get_fignums()
l.reverse()
for a in l:
plt.close(a)
Whenever you are plotting multiple figures, do not use plt.show, create your figures separately in a figure instance and add an Axes using add_subplot. Here is an example:
import matplotlib.pyplot as plt
fig1 = plt.figure()
ax1 = fig1.add_subplot(211) # the first subplot in the first figure
ax1.plot([1,2,3])
ax2 = fig1.add_subplot(212) # the second subplot in the first figure
ax2.plot([4,5,6])
plt.suptitle('Easy as 1,2,3')
fig1.show()
fig2 = plt.figure()
ax3 = fig2.add_subplot(211) # the first subplot in the second figure
ax3.plot([4,5,6])
ax4 = fig2.add_subplot(212) # the second subplot in the second figure
ax4.plot([4,5,6])
plt.suptitle('Easy as 1,2,3')
fig2.show()
By doing so you can still use your python shell even when the plots are active. This is the best way to plot multiple plots.

Resources