matplotlib .title() beheben: TypeError: 'Text' object is not callable

English Deutsch

Problem:

Der Titel eines matplotlib-Plots soll mit .title("Mein Titel") gesetzt werden, aber es wird eine Fehlermeldung wie folgende angezeigt

matplotlib_title_trace.txt
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f5f930f00eac> in <module>
---> 10 axs[0].title("My title")

TypeError: 'Text' object is not callable

Lösung

.set_title("Mein Titel") anstelle von .title("Mein Titel") verwenden!

Während

matplotlib_title_set_example.py
from matplotlib import pyplot as plt

plt.title("My title")

funktioniert, muss bei einem Axes-Objekt wie dem von plt.subplots() zurückgegebenen set_title() verwendet werden!

matplotlib_title_axes_example.py
from matplotlib import pyplot as plt
fig, axs = plt.subplots(2, 1)
# ...
axs[0].set_title("My title")

Check out similar posts by category: Python