将名称分配给可用于使图形成为当前图形的 matplotlib 子图图形
assigning name to matplotlib subplot figure that can be used to make figure current
我的问题与 here 提出的问题非常相似,但我希望能够再次使命名图形成为当前图形,而另一个问题的解决方案并未解决。这是一个尝试:
import matplotlib.pyplot as plt
plt.ion()
f, ax = plt.subplots()
f.canvas.set_window_title('My Window Title')
# make another figure, which will become current
plt.figure()
# attempts to make previous figure current, but instead creates third figure
plt.figure('My Window Title')
有没有办法为使用 subplots() 创建的图形指定一个名称,以便以后引用?
我已经弄明白了。答案是把f,ax=plt.subplots()
这一行分开,用add_subplot命令:
import matplotlib.pyplot as plt
plt.ion()
f = plt.figure('My Window Title')
ax = f.add_subplot(1,2,1)
# ...
# make another figure, which will become current
plt.figure()
# make previous figure current
plt.figure('My Window Title')
如果您想继续使用 plt.subplots()
,您可以使用关键字参数为图形命名。 subplots
未使用的任何 kwargs 都会传递给 plt.figure
,因此在这种情况下,我们要设置 num
选项(这就是您在示例中设置为 'My Window Title'
的内容:
f, ax = plt.subplots(num='My Window Title')
我的问题与 here 提出的问题非常相似,但我希望能够再次使命名图形成为当前图形,而另一个问题的解决方案并未解决。这是一个尝试:
import matplotlib.pyplot as plt
plt.ion()
f, ax = plt.subplots()
f.canvas.set_window_title('My Window Title')
# make another figure, which will become current
plt.figure()
# attempts to make previous figure current, but instead creates third figure
plt.figure('My Window Title')
有没有办法为使用 subplots() 创建的图形指定一个名称,以便以后引用?
我已经弄明白了。答案是把f,ax=plt.subplots()
这一行分开,用add_subplot命令:
import matplotlib.pyplot as plt
plt.ion()
f = plt.figure('My Window Title')
ax = f.add_subplot(1,2,1)
# ...
# make another figure, which will become current
plt.figure()
# make previous figure current
plt.figure('My Window Title')
如果您想继续使用 plt.subplots()
,您可以使用关键字参数为图形命名。 subplots
未使用的任何 kwargs 都会传递给 plt.figure
,因此在这种情况下,我们要设置 num
选项(这就是您在示例中设置为 'My Window Title'
的内容:
f, ax = plt.subplots(num='My Window Title')