RuntimeError:使用matplotlib 1.5时,不能将单个艺术家放在多个图中

时间:2016-06-20 18:04:43

标签: python matplotlib

这是我的代码:

import matplotlib.pyplot as plt
plt.figure(1)                # the first figure
plt.subplot(211)             # the first subplot in the first figure
plt.plot([1, 2, 3])
plt.subplot(212)             # the second subplot in the first figure
plt.plot([4, 5, 6])

plt.figure(2)                # a second figure
plt.plot([4, 5, 6])          # creates a subplot(111) by default
plt.text(.5,1.5,'211',figure = 211) #tring to add text in previous subplot

plt.figure(1)                # figure 1 current; subplot(212) still current
plt.subplot(211)             # make subplot(211) in figure1 current
plt.title('Easy as 1, 2, 3') # subplot 211 title

错误:

Traceback (most recent call last):
File "C:/Users/ezhou/Desktop/python/test3.py", line 11, in <module>
plt.text(.5,1.5,'211',figure = 211)
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 3567, in text
ret = gca().text(x, y, s, fontdict=fontdict, withdash=withdash, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 619, in text
self._add_text(t)
File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 1720, in _add_text
self._set_artist_props(txt)
File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 861, in _set_artist_props
a.set_figure(self.figure)
File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 640, in set_figure
raise RuntimeError("Can not put single artist in "
RuntimeError: Can not put single artist in more than one figure

我试图了解kwargs&#39;数字&#39;在类matplotlib.text.Text()中,但它总是回复&#39;不能将单个艺术家放在多个数字中#39;所以我很困惑如何使用这个&#39;数字&#39; kwarg。有人能给我一些建议吗?谢谢!

2 个答案:

答案 0 :(得分:4)

你不应该将数字作为kwarg传递,而是使用Figure(或Axes)实例的text方法。例如:

import matplotlib.pyplot as plt
fig1, fig2 = plt.figure(1), plt.figure(2)
sp1, sp2 = fig1.add_subplot(211), fig2.add_subplot(211)
sp1.plot([1, 2, 3])
sp2.plot([0, 1, 3])

fig1.text(.5, .3, 'whole figure')
sp2.text(.5, .5, 'subplot')

请注意,坐标是相对的(0,1)。

如果您发现matplotlib不必要地复杂化(就像我一样),您可能希望查看Plotly

答案 1 :(得分:0)

我完全坚持这一点。我的理解是matplotlib.figure用于在GUI中嵌入图形,然后能够在保持帧不变的同时更新信息?这将(对于我在python 2.7上,我认为它在Python 3+中的tkinter)运行时没有错误并产生相同的图形。但我根本不知道为什么他们提供了使用plt来更新此图上的文字的选项,而文字并未显示。明天我会再看一遍,但是我很高兴有人在此期间纠正我,因为文档令人困惑。

import matplotlib
import Tkinter as tk 
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
matplotlib.use("TkAgg")

first_figure = Figure()                
plot1_sub1 = first_figure.add_subplot(211)
plot1_sub2 = first_figure.add_subplot(212)

second_figure = Figure()
plot2_sub1 = second_figure.add_subplot(111)

class MainExample(tk.Tk): 

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Example")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        frame = Example(container, self)
        self.frames[Example] = frame
        frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(Example)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class Example(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.canvas = FigureCanvasTkAgg(first_figure, self)
        self.canvas.get_tk_widget().pack()

        self.toolbar = NavigationToolbar2TkAgg(self.canvas, self)
        self.toolbar.update()
        self.canvas._tkcanvas.pack(fill=tk.BOTH, expand = True)

        plot1_sub1.plot([1, 2, 3])
        plot1_sub2.plot([4, 5, 6])

        plot2_sub1.plot([4, 5, 6])

        plt.text(.5,1.5,'211',figure=plot1_sub1)

if __name__ == '__main__':
    app = MainExample()
    app.geometry("1280x720")
    app.mainloop()