运行时错误: 使用matplotlib 1.5时,无法将单个艺术家放置在多个图形中

2024-05-17 19:43:17 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

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

我试图理解matplotlib.text.text()类中的kwargs“figure”,但它总是回答“不能将单个艺术家放在多个图形中”。所以我对如何使用这个“数字”kwarg感到困惑。有人能给我一些建议吗?谢谢!


Tags: thetextinpyartistmatplotliblibpackages
2条回答

我完全被困在这上面了。我的理解是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()

不应将figure作为kwarg传递,而应使用figure(或Axes)实例的文本方法。示例:

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

相关问题 更多 >