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

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()

Related

matplotlib plot window not showing in windows 11

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.

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/

How make row size of seaborn clustermap equal? [duplicate]

When plotting heatmaps with seaborn (and correlation matrices with matplotlib) the first and the last row is cut in halve.
This happens also when I run this minimal code example which I found online.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.read_csv('https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv')
plt.figure(figsize=(10,5))
sns.heatmap(data.corr())
plt.show()
The labels at the y axis are on the correct spot, but the rows aren't completely there.
A few days ago, it work as intended. Since then, I installed texlive-xetex so I removed it again but it didn't solve my problem.
Any ideas what I could be missing?
Unfortunately matplotlib 3.1.1 broke seaborn heatmaps; and in general inverted axes with fixed ticks.
This is fixed in the current development version; you may hence
revert to matplotlib 3.1.0
use matplotlib 3.1.2 or higher
set the heatmap limits manually (ax.set_ylim(bottom, top) # set the ylim to bottom, top)
Its a bug in the matplotlib regression between 3.1.0 and 3.1.1
You can correct this by:
import seaborn as sns
df_corr = someDataFrame.corr()
ax = sns.heatmap(df_corr, annot=True) #notation: "annot" not "annote"
bottom, top = ax.get_ylim()
ax.set_ylim(bottom + 0.5, top - 0.5)
Fixed using the above and setting the heatmap limits manually.
First
ax = sns.heatmap(...
checked the current axes with
ax.get_ylim()
(5.5, 0.5)
Fixed with
ax.set_ylim(6.0, 0)
I solved it by adding this line in my code, with matplotlib==3.1.1:
ax.set_ylim(sorted(ax.get_xlim(), reverse=True))
NB. The only reason this works is because the x-axis isn't changed, so use at your own risk with future mpl versions
matplotlib 3.1.2 is out -
It is available in the Anaconda cloud via conda-forge but I was not able to install it via conda install.
The manual alternative worked:
Download matplotlib 3.1.2 from github and install via pip
% curl https://codeload.github.com/matplotlib/matplotlib/tar.gz/v3.1.2 --output matplotlib-3.1.2.tar.gz
% pip install matplotlib-3.1.2.tar.gz
Worked for me:
b, t = plt.ylim()
b += 0.5
t -= 0.5
custom_ylim = (b, t)
plt.setp(axes, ylim=custom_ylim)
It happens with matplotlib version 3.1.1 as suggested by importanceofbeingernest
Following solved my problem
pip install matplotlib==3.1.0
rustyDev is right about conda-forge, but I did not need to do a manual pip install from a github download. For me, on Windows, it worked directly. And the plots are all nice again.
https://anaconda.org/conda-forge/matplotlib
conda install -c conda-forge matplotlib
optional points, not needed for the answer:
Afterwards, I tried other steps, but they are not needed: In conda prompt: conda search matplotlib --info showed no new version info, the most recent info was for 3.1.1. Thus I tried pip using pip install matplotlib==3.1.2 But pip says "Requirement already satisfied"
Then getting the version according to medium.com/#rakshithvasudev/… python - import matplotlib - matplotlib.__version__ shows that 3.1.2 was successfully installed
Btw, I had this error directly after updating Spyder to v4.0.0. The error was in a plot of a confusion matrix. This was mentioned already some months ago. stackoverflow.com/questions/57225685/… which is already linked to this seaborn question.
Downgrade your matplotlib
!pip install matplotlib==3.1.0
and add this line to your plot code :
ax[i].set_ylim(sorted(ax[i].get_xlim(), reverse=True))
As #ImportanceOfBeingErnest mentioned, this issue is due to broken seaborn heatmaps in a specific version of matplotlib so simple solution to this problem is to upgrade matplotlib as follows:
pip install --upgrade matplotlib

How to fix cells cut in Seaborn clustermap [duplicate]

When plotting heatmaps with seaborn (and correlation matrices with matplotlib) the first and the last row is cut in halve.
This happens also when I run this minimal code example which I found online.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.read_csv('https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv')
plt.figure(figsize=(10,5))
sns.heatmap(data.corr())
plt.show()
The labels at the y axis are on the correct spot, but the rows aren't completely there.
A few days ago, it work as intended. Since then, I installed texlive-xetex so I removed it again but it didn't solve my problem.
Any ideas what I could be missing?
Unfortunately matplotlib 3.1.1 broke seaborn heatmaps; and in general inverted axes with fixed ticks.
This is fixed in the current development version; you may hence
revert to matplotlib 3.1.0
use matplotlib 3.1.2 or higher
set the heatmap limits manually (ax.set_ylim(bottom, top) # set the ylim to bottom, top)
Its a bug in the matplotlib regression between 3.1.0 and 3.1.1
You can correct this by:
import seaborn as sns
df_corr = someDataFrame.corr()
ax = sns.heatmap(df_corr, annot=True) #notation: "annot" not "annote"
bottom, top = ax.get_ylim()
ax.set_ylim(bottom + 0.5, top - 0.5)
Fixed using the above and setting the heatmap limits manually.
First
ax = sns.heatmap(...
checked the current axes with
ax.get_ylim()
(5.5, 0.5)
Fixed with
ax.set_ylim(6.0, 0)
I solved it by adding this line in my code, with matplotlib==3.1.1:
ax.set_ylim(sorted(ax.get_xlim(), reverse=True))
NB. The only reason this works is because the x-axis isn't changed, so use at your own risk with future mpl versions
matplotlib 3.1.2 is out -
It is available in the Anaconda cloud via conda-forge but I was not able to install it via conda install.
The manual alternative worked:
Download matplotlib 3.1.2 from github and install via pip
% curl https://codeload.github.com/matplotlib/matplotlib/tar.gz/v3.1.2 --output matplotlib-3.1.2.tar.gz
% pip install matplotlib-3.1.2.tar.gz
Worked for me:
b, t = plt.ylim()
b += 0.5
t -= 0.5
custom_ylim = (b, t)
plt.setp(axes, ylim=custom_ylim)
It happens with matplotlib version 3.1.1 as suggested by importanceofbeingernest
Following solved my problem
pip install matplotlib==3.1.0
rustyDev is right about conda-forge, but I did not need to do a manual pip install from a github download. For me, on Windows, it worked directly. And the plots are all nice again.
https://anaconda.org/conda-forge/matplotlib
conda install -c conda-forge matplotlib
optional points, not needed for the answer:
Afterwards, I tried other steps, but they are not needed: In conda prompt: conda search matplotlib --info showed no new version info, the most recent info was for 3.1.1. Thus I tried pip using pip install matplotlib==3.1.2 But pip says "Requirement already satisfied"
Then getting the version according to medium.com/#rakshithvasudev/… python - import matplotlib - matplotlib.__version__ shows that 3.1.2 was successfully installed
Btw, I had this error directly after updating Spyder to v4.0.0. The error was in a plot of a confusion matrix. This was mentioned already some months ago. stackoverflow.com/questions/57225685/… which is already linked to this seaborn question.
Downgrade your matplotlib
!pip install matplotlib==3.1.0
and add this line to your plot code :
ax[i].set_ylim(sorted(ax[i].get_xlim(), reverse=True))
As #ImportanceOfBeingErnest mentioned, this issue is due to broken seaborn heatmaps in a specific version of matplotlib so simple solution to this problem is to upgrade matplotlib as follows:
pip install --upgrade matplotlib

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