Changing matplotlib window title is throwing strange error - python

I'm using python 3.6 in Jupyter lab on a linux mint machine to run this snippet of code
import matplotlib.pyplot as plt
fig = plt.figure()
man = plt.get_current_fig_manager()
man.window.setWindowTitle("New Title")
..it returns the following error message:
AttributeError: 'FigureManagerBase' object has no attribute 'window'
I have checked the GUI backends. All are available and all of them return this error even if I force the backend using ...
import matplotlib
matplotlib.use(<gui>,warn=False, force=True)
...before importing pyplot. The code has been working OK in Spyder but I've had to move to Jupyter. The matplotlib docs say that FigureManagerBase attributes include 'window'. I'm stumped

To change the window title, use the code below:
import matplotlib.pyplot as plt
fig = plt.figure()
man = plt.get_current_fig_manager()
man.canvas.set_window_title("New Title")

Using python 3.8, matplotlib 3.5.3 the following works for me:
import matplotlib.pyplot as plt
fig = plt.figure()
man = plt.get_current_fig_manager()
man.set_window_title("New Title")

Related

Matplotlib FuncAnimation not plotting any chart inside Jupyter Notebook

Simple matplotlib plot. Here is my code
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from itertools import count
import random
x = []
y = []
index=count()
def animate(i):
x.append(next(index))
y.append(random.randint(0,10))
plt.plot(x,y)
a = FuncAnimation(plt.gcf(),animate,interval=1000)
plt.tight_layout()
plt.show()
Running the code above I get
<Figure size 576x396 with 0 Axes>
but no chart appears.
Are you using Jupyter notebooks to run it? I tried with native libraries and it works just fine. The plots are visible.
Checking here i see the same situation. Could you try to use %matplotlib inline before importing matplotlib as:
%matplotlib inline # this line before importing matplotlib
from matplotlib import pyplot as plt
That said, the animation can be displayed using JavaScript. This is similar to the ani.to_html5() solution, except that it does not require any video codecs.
from IPython.display import HTML
HTML(a.to_jshtml())
this answer brings a more complete overview...

Matplotlibs pyplot.subplots() crashes kernel

Using matplotlibs pyplot (in this case imported as plt) crashes my kernel (Py3.5 under Win7) when using more than 1 plot. More specifically, the axes obejct results in a crash. The crash is immediatly and killing all running python instances.
E.g. calling
fig,ax = plt.subplots(2,2)
crashes.
While
ret = plt.subplots(2,2)
fig = ret[0]
works, and then crashes when calling
ax = ret[1]
or
ax1 = ret[1][0]
or similar
Is this a known issue?
I too faced a similar issue but I could get rid of the crashing problem using this way.
import matplotlib
matplotlib.use("TKAgg")
import matplotlib.pyplot as plt
fig,ax=plt.subplots(1, 1)
This worked for me. Give it a try.
Hope it helps.
Best Wishes

spyder won't show the matplotlib window

i am using spyder to run the following script
import matplotlib.pyplot as plt
fig = plt.figure()
ax=fig.add_subplot(111)
ax.plot([2,1,3])
plt.ion()
plt.show()
input('something')
the problem is, that the matplotlib-window appears at the end of the script (after i entered 'something')
when i'm executing this script in a terminal the window appears and i can enter 'something' simultaneously - this is what i need.
does someone has an idea what causes this problem?

pyplot.show function in spyder with python 3.4

I'm a novice with Python and working through "Machine Learning In Action" by P. Harrington. I'm having issues seeing my plot when using the pyplot.show() function. Below is the code that I'm entering in the IPython console on the bottom right of Spyder.
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(datingDataMat[:,1], datingDataMat[:,2])
plt.show()
After I enter this last line, nothing happens. Does anyone have an idea why it doesn't appear?

Matplotlib with Eclipse PyDev is not plotting

I have the following problem when using Matplotlib in Eclipse with PyDev.
import datetime
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn
...
# Plot results
fig = plt.figure()
fig.patch.set_facecolor('white')
# Plot the price of the SPY ETF
ax1 = fig.add_subplot(211, ylabel='SPY ETF price in $')
bars['Close'].plot(ax=ax1, color='r', lw=2.)
# Plot the equity curve
ax2 = fig.add_subplot(212, ylabel='Portfolio value in $')
returns['total'].plot(ax=ax2, lw=2.)
fig.show()
I left out the calculations in between.
What happens when I run the program is that the Python Launcher starts and then disappears after 1 second.
I tried to solve the problem on Mac OSX and on Ubuntu and in both cases it did not work.
When using IDLE on the other hand it works and it will come out the following output:
In general the matplotlib works an Eclipse as I get displayed the output of the following code:
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
I also set my backend to template in ~/.matplotlib/matplotlibrc, to
backend : Qt4Agg
Maybe someone has an idea why I can not plot this in Eclipse.
Thank you!

Categories