matplotlib randomly shows no plot in figure window - python-3.x

I am using Python 3.8.16 on Ubuntu 18.04.6 LTS and work with matplotlib 3.6.2, but do not use Jupyter in this case. When I plot a graph with matplotlib, sometimes happens that I get the figure window, but without any content, except the window title "Figure 1". I cannot close the figure with "q", and usually cannot break the execution with ctrl C, however ctrl \ usually works. When I run the program again, usually everything is fine. I would say that the probability, that the plot will be shown properly is around 80-90%. I had the same behaviours with Python 3.7 and with previous versions of matplotlib - seems it is not version specific. The problem occures in diferent programs. The matplotlib.get_backend() command returns TkAgg, and the posts in discussion groups suggest it is ok in case of Ubuntu. I couldn't find any solutions on internet forums so would appreciate any hint.

Related

Pyautogui image recognition returns None no matter what I do

I have been trying to get pyautogui to locate graphically a folder on my desktop but it fails no matter what I do:
This is my code (int_auto.py)
import pyautogui
button = pyautogui.locateCenterOnScreen('/Users/cadellteng/Desktop/Test-ground/randompy/pyxell2.png')
print(button)
I literally read through every SO thread I could find on this topic and some suggested that the photo needs to be lossless, so I used the cmd+shift+4 command on Mac to achieve this. A screenshot of my desktop and the reference image I used is attached here.
Note: This photo was converted to jpg because SO only allows photos up to 2MiB to be uploaded
Other things I tried are:
1. using locateAllOnScreen
2. grayscale = True
3. uninstall pyautogui and reinstalling pyautogui
But no matter what I did, I couldn't seems to be able to get the outcome I want. If it helps, I use a double screen and my code editor (VS Code) is on the secondary screen. What you see here as my desktop is my primary screen.
When the program is running nothing is blocking the folder.
There is also a warning on my terminal which I'm not sure if it's going to be useful:
/Users/cadellteng/Desktop/Test-ground/randompy/env/lib/python3.8/site-packages/rubicon/objc/ctypes_patch.py:21:
UserWarning: rubicon.objc.ctypes_patch has only been tested with Python 3.4 through 3.7.
You are using Python 3.8.2. Most likely things will work properly, but you may experience crashes if Python's internals have changed significantly. warnings.warn(
Do let me know if you require additional information.
EDIT June 01, 2020: So I thought that maybe pyautogui was not able to detect the folder because there are multiple folders and they all look the same and was not able to detect it. So I got this image off the web and tried to search it with it opened on my desktop with nothing else blocking it. But even then it was not able to find it.
I'm not sure if I am doing something wrong here but with each try, I am becoming more convinced that this API is broken.
The size of the picture might be a problem. When I use locateOnScreen to find an element in a browser and zoom out by, for example, 10%, then it will not locate it on screen even if is almost the same.
You have a Macbook so I think its because of the pixel density of the screen and the resolution pyautogui detects. You can check by:
image = pyautogui.LocateCenterOnScreen(‘image.png’)
print(image)
If you see coordinates above 2000 its probably because of the resolution. I suggest a workaround if you want to click the image like:
pyautogui.click(image.x/2, image.y/2, clicks=1, interval=10, button=‘left’) also try cropping the images as small as possible!

How can Pycharm (with matplotlib) show me my graphs in the same Py charm, next to my code?. Not in additional windows

i am using Matplotlib with Pycharm in a Mac osx. When I am making a graph, and I code plt.show() pycharm opens another window. And if there is 2 graphs, to see the 2nd one I have to close the first one. And I want to see both together.
There is a option to show the two graph in the same window, where my code is?
Thanks very much.
This link may help:
IPython magic commands
Using Jupyter notebooks you can achieve what you want by using %matplotlib inline. It seems from the link above that PyCharm may have some way to enable this same functionality.
See also: Inline plots in the scientific view

running a Program from vim that opens another window messes up the formating of my terminal

Im using vim inside a gnome-Terminal to edit a Python Program. Im also using a tiling Window manager calles awesome-wm.
Inside my Python program im plotting some data to a matplotlib graph (when executed that opens another window). When i want to test my Program i use the following comand to run the Program from inside of vim.
:!python3 %
when the Program runs it prints three messages to the shell and opens a matplotlib window to plot the data.
Sometimes i can only see the messed up linebreaks and have to scroll up to see the messages.
My question now is why is the formating inside the console messed up an how do i fix it?
My guess would be that the Terminal Window does not notice that it is beeing resized when the plot window is beeing opend, but i dont know how to refresh the terminal in that case.
using :!clear; python3 % does not help.
Thanks in advance.
(Well, it isn't a direct solution to your problem, but here it goes.)
Most terminal don't handle reflow in resize properly. There is no right ways to do it so most don't attempt to pull all the tricks in the book to make this better. After all, for classic floating window manger, resizing isn't something you do very often. LibVTE/Gnome-terminal isn't designed for tiling WMs and has not been patched over the years to make it better.
Urxvt is probably the most common terminal for AwesomeWM (the default is xterm because it is much more commonly installed by default). It has more advanced features like tabs and transparency you expect from modern terminals. Note that some of them (tabs) are disabled by default.
Sometime using software like tmux can be used to mitigate some of the limitations of any given terminal, but it comes with limitations of its own.

Tkinter layout changes from Spyder to Command Prompt

I have built a simple GUI in tkinter that goes through an algorithm to fit Radial Velocity data. Everything works fine in Spyder, however when I try to run the file in command prompt the GUI's dimensions change drastically. This is how it is supposed to look and how it looks in Spyder. This is how it looks when it's run in command prompt. The plots that are produced show the same behavior described. I've tried setting all the windows in Tkinter so that they are unable to be resized. I'm not sure what else to try. Is there a way to fix this? I am on Windows 10 64bit, Python 3.5.4, and Spyder 3.2.5.
I found out what the problem was. It's due to the fact that Spyder automatically overrides high DPI scaling, whereas both Python.exe and Pythonw.exe do not. The solution was to check the 'Override High DPI Scaling behavior' option under the compatibility tab for both python.exe and pythonw.exe.

bad screen distance

I'm trying to obtain an figure object (using matplotlib), inside a wxpython application. I don't need to plot (or see) the figure, because I would want only to save it in a png file.
When I do it using Windows XP, it is all okay, if I do it outside the wxpython application, for example inside sphinx documentation, it's all okay again.
But If I do it inside my wxpython application... something goes wrong and it raises this:
_tkinter.TclError: bad screen distance "320.0"
Has someone any idea about?
Since you're trying to use wxPython and getting tkinter errors, you probably haven't told matplotlib to use wx. This can either be done in the matplotlibrc file, or in the program. Instructions are here.

Resources