jupyter笔记本matplotlib显示绘图,然后在原始图形上绘图

jupyter笔记本matplotlib显示绘图,然后在原始图形上绘图,matplotlib,jupyter-notebook,Matplotlib,Jupyter Notebook,我想画一个有两个轴的白色图,向用户显示,然后向有两个轴的白色图添加一条线,向用户显示,然后向线添加一些点,然后向用户显示。我如何做到这一点,而不复制代码一次又一次 我现在做的是在第一个代码块中 import math import matplotlib.pyplot as plt import numpy as np %matplotlib inline fig = plt.figure(figsize=(5,5)) ax = plt.axes() ax.set_xlabel('cat')

我想画一个有两个轴的白色图,向用户显示,然后向有两个轴的白色图添加一条线,向用户显示,然后向线添加一些点,然后向用户显示。我如何做到这一点,而不复制代码一次又一次

我现在做的是在第一个代码块中

import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(5,5))
ax = plt.axes()

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")

plt.show()
fig = plt.figure(figsize=(5,5))
ax = plt.axes()

x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")
plt.show()
fig = plt.figure(figsize=(5,5))
ax = plt.axes()

x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")
p0 = 0.5
p1 = 0.5
color = "blue"
textd =0.05
ax.scatter([p0],[p1], color = color,zorder=1)
ax.text(p0+textd, p1+textd, 'tiger',color = color,zorder =2)
plt.show()
import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(5,5))
ax = plt.axes()

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")

plt.show()
add line directly without duplicating the code for making axes
plt.show()
add point directly without duplicating the code for making axes and lines
plt.show()
然后在第二个代码块中

import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(5,5))
ax = plt.axes()

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")

plt.show()
fig = plt.figure(figsize=(5,5))
ax = plt.axes()

x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")
plt.show()
fig = plt.figure(figsize=(5,5))
ax = plt.axes()

x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")
p0 = 0.5
p1 = 0.5
color = "blue"
textd =0.05
ax.scatter([p0],[p1], color = color,zorder=1)
ax.text(p0+textd, p1+textd, 'tiger',color = color,zorder =2)
plt.show()
import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(5,5))
ax = plt.axes()

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")

plt.show()
add line directly without duplicating the code for making axes
plt.show()
add point directly without duplicating the code for making axes and lines
plt.show()
然后在第三个代码块中

import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(5,5))
ax = plt.axes()

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")

plt.show()
fig = plt.figure(figsize=(5,5))
ax = plt.axes()

x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")
plt.show()
fig = plt.figure(figsize=(5,5))
ax = plt.axes()

x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")
p0 = 0.5
p1 = 0.5
color = "blue"
textd =0.05
ax.scatter([p0],[p1], color = color,zorder=1)
ax.text(p0+textd, p1+textd, 'tiger',color = color,zorder =2)
plt.show()
import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(5,5))
ax = plt.axes()

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")

plt.show()
add line directly without duplicating the code for making axes
plt.show()
add point directly without duplicating the code for making axes and lines
plt.show()
我要找的是第一个代码块中的内容

import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(5,5))
ax = plt.axes()

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")

plt.show()
fig = plt.figure(figsize=(5,5))
ax = plt.axes()

x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")
plt.show()
fig = plt.figure(figsize=(5,5))
ax = plt.axes()

x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")
p0 = 0.5
p1 = 0.5
color = "blue"
textd =0.05
ax.scatter([p0],[p1], color = color,zorder=1)
ax.text(p0+textd, p1+textd, 'tiger',color = color,zorder =2)
plt.show()
import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(5,5))
ax = plt.axes()

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")

plt.show()
add line directly without duplicating the code for making axes
plt.show()
add point directly without duplicating the code for making axes and lines
plt.show()
然后在第二个代码块中

import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(5,5))
ax = plt.axes()

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")

plt.show()
fig = plt.figure(figsize=(5,5))
ax = plt.axes()

x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")
plt.show()
fig = plt.figure(figsize=(5,5))
ax = plt.axes()

x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")
p0 = 0.5
p1 = 0.5
color = "blue"
textd =0.05
ax.scatter([p0],[p1], color = color,zorder=1)
ax.text(p0+textd, p1+textd, 'tiger',color = color,zorder =2)
plt.show()
import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(5,5))
ax = plt.axes()

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")

plt.show()
add line directly without duplicating the code for making axes
plt.show()
add point directly without duplicating the code for making axes and lines
plt.show()
然后在第三个代码块中

import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(5,5))
ax = plt.axes()

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")

plt.show()
fig = plt.figure(figsize=(5,5))
ax = plt.axes()

x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")
plt.show()
fig = plt.figure(figsize=(5,5))
ax = plt.axes()

x = np.linspace(0, 1.0, 1000)
ax.plot(x, 1.0-x,zorder = 0)

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")
p0 = 0.5
p1 = 0.5
color = "blue"
textd =0.05
ax.scatter([p0],[p1], color = color,zorder=1)
ax.text(p0+textd, p1+textd, 'tiger',color = color,zorder =2)
plt.show()
import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(5,5))
ax = plt.axes()

ax.set_xlabel('cat')
ax.set_ylabel('dog')

plt.title("Set of 2 animals")

plt.show()
add line directly without duplicating the code for making axes
plt.show()
add point directly without duplicating the code for making axes and lines
plt.show()

更新:我真的找到了答案

def plot(step):
  fig = plt.figure(figsize=(5,5))
  ax = plt.axes()
  ax.set_xlabel('cat')
  ax.set_ylabel('dog')
  plt.title("Set of 2 animals")
  if step>=1:
    x = np.linspace(0, 1.0, 1000)
    ax.plot(x, 1.0-x,zorder = 0)
  if step>=2:
    p0 = 0.5
    p1 = 0.5
    color = "blue"
    textd =0.05
    ax.scatter([p0],[p1], color = color,zorder=1)
    ax.text(p0+textd, p1+textd, 'tiger',color = color,zorder =2)
  plot.show()

应该能够解决这个问题。

我会尽量不在每个新代码块中生成新数字。无论如何,您需要确保所有代码块都与内联后端中的代码块位于同一个单元格中,并且在单元格的末尾关闭execution@lanhi但我不能,我想一步一步演示如何创建这些绘图这能回答你的问题吗?