+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

plt.gcf() doesn't show previous instance of a plot

发布于2021-03-08 20:45     阅读(34)     评论(0)     点赞(26)     收藏(3)


0

1

2

3

4

5

6

7

8

9

I want to save an instance of a plot into an object so that I can display it later by just calling that object.

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 10, 0.1)
y1 = x
y2 = np.sin(x)

plt.plot(x, y1, linewidth=1, color = 'deepskyblue')
fig1 = plt.gcf()

plt.plot(x, y2, linewidth=1, color = 'red')
fig2 = plt.gcf()

In this example, I first draw a blue line (y1=x) and use plt.gcf() to save an instance of this plot in fig1. Then I add a red curve (y2=sin(x)) to the plot and use plt.gcf() again to save this plot in fig2. Now, I expect that when I call fig1 I only get the blue line, and when I call fig2 I get both lines. Like this (I'm in Jupyter):

fig1   # or fig1.show() if not in Jupyter

Only blue curve
Only blue curve

fig2 

Both curves
Both curves

But, in reality, when I call fig1 and fig2, both of them show both curves (like the second picture). Can someone please help how I can correctly get an instance of each plot so that I can display each of them later whenever I want?


解决方案


You need to force matplotlib to actually draw the figure by setting a plot.show() in your code:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 10, 0.1)
y1 = x
y2 = np.sin(x)

plt.plot(x, y1, linewidth=1, color = 'deepskyblue')
plt.show()
fig1 = plt.gcf()

plt.plot(x, y2, linewidth=1, color = 'red')
plt.show()
fig2 = plt.gcf()

Using the function plt.plot() always plots to the current axis (if no axis is present, a new one is created). You can also tell matplotlib explicitly to open a new figure:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 10, 0.1)
y1 = x
y2 = np.sin(x)

# open first figure
fig1 = plt.figure()
# plot
plt.plot(x, y1, linewidth=1, color = 'deepskyblue')

# open second figure
fig2 = plt.figure()
#plot
plt.plot(x, y2, linewidth=1, color = 'red')

Although this already fixes your problem, it is considered good practice to use an object-oriented version of this like this:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 10, 0.1)
y1 = x
y2 = np.sin(x)

# open first figure, axis
fig1, ax1 = plt.subplots()
# plot
ax1.plot(x, y1, linewidth=1, color = 'deepskyblue')

# open second figure, axis
fig2, ax2 = plt.subplots()
#plot
ax2.plot(x, y2, linewidth=1, color = 'red')

In all cases, you will get: output

Now, why don't you need plt.show() in the other approaches? Well, matplotlib by explicitly opening a new figure/axis, it is obvious that the previous axis is finished and can be drawn.

The last approach is the clearest as you tell exactly which figure and which axis you are considering.

0

1

2

3

4

5

6

7



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/880001/0963b625894850fcedb8/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

26 0
收藏该文
已收藏

评论内容:(最多支持255个字符)