小编rmn*_*oss的帖子

matplotlib:如何获取现有 twinx() 轴的句柄?

我想创建一个带有两个 y 轴的图形,并在我的代码中的不同点(来自不同的函数)向每个轴添加多条曲线。

在第一个函数中,我创建了一个图形:

   import matplotlib.pyplot as plt
   from numpy import *

   # Opens new figure with two axes
   def function1():
          f = plt.figure(1)
          ax1 = plt.subplot(211)
          ax2 = ax1.twinx()  

          x = linspace(0,2*pi,100)
          ax1.plot(x,sin(x),'b')
          ax2.plot(x,1000*cos(x),'g')

   # other stuff will be shown in subplot 212...
Run Code Online (Sandbox Code Playgroud)

现在,在第二个函数中,我想为每个已创建的轴添加一条曲线:

   def function2():
          # Get handles of figure, which is already open
          f = plt.figure(1)
          ax3 = plt.subplot(211).axes  # get handle to 1st axis
          ax4 = ax3.twinx()            # get handle to 2nd axis (wrong?)

          # Add …
Run Code Online (Sandbox Code Playgroud)

python plot matplotlib

5
推荐指数
1
解决办法
1561
查看次数

标签 统计

matplotlib ×1

plot ×1

python ×1