matplotlib plot window not showing in windows 11 - python

I have recently upgraded my OS to windows 11, and installed conda as the python package manager. I am using windows terminal(v1.11) with powershell (v7.2), and python(v3.9), matplotlib(v3.5).
When trying to plot anything using matplotlib.pyplot, the plot window is not showing up anywhere.
I tried to specify the backend to be "qtagg" but it has no effect.
These is the python code
import matplotlib.pyplot as plt
plt.plot(1,1)
plt.show()
Does windows support pyplot window? Or some packages or setting are missing? Thank you very much.

Related

UserWarning: Matplotlib is currently using agg, so cannot show the figure

I'm trying to run a basic matplotlib example from the official website:
However, when i run the code, my Python interpreter complains and outputs the following message:
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()
I've installed matplotlib via pip3 install matplotlib.
My current python3 version is 3.9.1 and my OS is Ubuntu 20.04.
I've already tried installing tkinter, as already described here, with no success.
What should I do? Why is this happening?
Please try these, if any works for you:
if you are using Jupyter Notebook
%matplotlib inline
make sure you have tkinter, recompile python interpreter after installing tkinter
try:
import matplotlib
import matplotlib.pyplot as plt
plt.style.use('ggplot')
matplotlib.use( 'tkagg' )
x = [1, 5, 1.5, 4]
y = [9, 1.8, 8, 11]
plt.scatter(x,y)
plt.show()

How do I get Pyinstaller to work with pyplot?

I am trying to turn a program, that uses the matplotlib.pyplot module, into an executable.
The script itself works as intended when run in the terminal, but when I use the executable made by pyinstaller, it shows - "Script failed to execute".
I have narrowed down the issue to the following code:
import matplotlib.pyplot as plt
The .exe will run fine with this code commented out, but that way I am not able to plot graphs using pyplot.
EDIT: I did some research on this and found out it's a known issue with Matplotlib 3.3, so I was able to get around it by uninstalling the current version and using:
pip install matplotlib==3.1.3

Matplotlib does not show up on ubuntu windows subsystem

I have installed matplotlib on ubuntu windows subsystem and run this simple code:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
plt.plot([1, 2, 3, 4])
plt.show()
But it just does not show up anything.
So I tryied to install matplotlib on windows powershell and then executed the same code and it worked well:
Well even know it worked fine using powershell I really want to run my programs using linux susbystem, what should I do?
WSL does not support graphics.
You need to setup X server for WSL.
Have a look at https://x410.dev/

Import package works in jupyter notebook but not in iPython and Anaconda cmd

When searching about this issue, I came across some questions asking the opposite, i.e., package opens in iPython but not in Jupyter Notebook. But in my case, its the opposite. That's why I posted this question.
I added path\to\anaconda3 and path\to\anaconda3\Lib\site-packages in the environment variable, but it doesn't solve the issue.
I can see the packages in the site-packages folder:
But I just can't import some of the packages in iPython:
or with python in the anaconda cmd:
But it works fine in Jupyter Notebook:
What do/can I do to fix this?
Here's some more info if it helps:
(base) C:\Users\h473>where python
C:\Users\h473\AppData\Local\Continuum\anaconda3\python.exe
(base) C:\Users\h473>where conda
C:\Users\h473\AppData\Local\Continuum\anaconda3\Library\bin\conda.bat
C:\Users\h473\AppData\Local\Continuum\anaconda3\Scripts\conda.exe
(base) C:\Users\h473>where pip
C:\Users\h473\AppData\Local\Continuum\anaconda3\Scripts\pip.exe
P.S.: It doesn't happen for all packages, only some packages, as shown for pandas, numpy and matplotlib in the screenshot below.
When you are using matplotlib (and seaborn is built on top of it) it needs to use a so called backend that is used to display the actual GUI with the plot in it once you execute for example matplotlib.pyplot.show().
When you are running a Jupyter Notebook with matplotlib in inline mode (default I think, but not sure), then a Jupyter specific backend is used (module://ipykernel.pylab.backend_inline). That makes sense, since the plots should not appear in separate windows, but be displayed inside the notebook itself.
When you are in an interactive python or iPython session however, Qt5 was used as
import matplotlib
print(matplotlib.rcParams["backend"]) # this prints the backend that would be loaded when trying anything with pyplot
has revealed. Since you get the error youa re getting, it looks like your QT5 installation is broken. You can try to reinstall them using the conda commands, but for now you could also fall back to using a different backend, that you need to specify before trying to load seaborn:
import matplotlib
matplotlib.use("TkAgg") #use backend TkAgg
import seaborn
You can also change the default backend being loaded to TkAgg by creating a matplotlibrc file in C:\Users\<your name>\.matplotlib\ with
backend : TkAgg
in it.

pyplot while using X11 forwarding

I'm trying to setup my remote working environment so that I can use matplotlib.pyplot under ipython. My local machine and remote machine are both MAC OSX Mavericks. I've tried xeyes and it works properly so I think my X11 forwarding works fine. However, when I try functions in matplotlib.pyplot it didn't show any error message but I didn't see any figure either.
I know it's about which backend to use and I've tried using ipython --pylab=tk but didn't work. Tried to install pygtk but can't manage to install a dependency py2cairo (seems to be an open issue). Any other thoughts? Thanks.
How did you "try functions in matplotlib.pyplot"?
The following works for me
import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.show()

Categories