matplotlib.axes.axes.twiny() - 创建与yaxis共享的两个Axes

matplotlib.axes.axes.twiny()

Matplotlib是Python中的一个库,它是NumPy库的数值-数学扩展。Axes包含了大多数图形元素:Axis、Tick、Line2D、Text、Polygon等,并设置坐标系。Axes的实例通过callbacks属性支持回调。

matplotlib.axes.axes.twiny()函数

matplotlib库的Axes模块中的Axes.twiny()函数用于创建与yaxis共享的两个Axes。

语法:Axes.twiny(self)

返回值:该方法用于返回以下内容。

  • ax_twin:返回新创建的Axes实例。

下面的例子演示了matplotlib.axes.axes.twiny()函数在matplotlib.axes中的作用:

示例1

# Implementation of matplotlib function
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
   
   
def GFG1(temp):
    return (5. / 9.) * (temp - 32)
   
def GFG2(ax1):
    y1, y2 = ax1.get_ylim()
    ax_twin .set_ylim(GFG1(y1), GFG1(y2))
    ax_twin .figure.canvas.draw()
   
fig, ax1 = plt.subplots()
ax_twin = ax1.twiny()
   
ax1.callbacks.connect("ylim_changed", GFG2)
ax1.plot(np.linspace(-40, 120, 100))
ax1.set_ylim(0, 100)
   
ax1.set_xlabel('Fahrenheit')
ax_twin .set_xlabel('Celsius')
  
fig.suptitle('matplotlib.axes.Axes.twiny()\
 function Example\n\n', fontweight ="bold")
plt.show()

输出:

matplotlib.axes.axes.twiny()

示例2

# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
  
# Create some mock data
t = np.arange(0.01, 10.0, 0.001)
data1 = np.exp(t)
data2 = np.sin(0.4 * np.pi * t)
  
fig, ax1 = plt.subplots()
  
color = 'tab:blue'
ax1.set_ylabel('time (s)')
ax1.set_xlabel('exp', color = color)
ax1.plot(data1, t, color = color)
ax1.tick_params(axis ='x', labelcolor = color)
  
ax2 = ax1.twiny()
  
color = 'tab:green'
ax2.set_xlabel('sin', color = color)
ax2.plot(data2, t, color = color)
ax2.tick_params(axis ='x', labelcolor = color)
  
fig.suptitle('matplotlib.axes.Axes.twiny()\
 function Example\n\n', fontweight ="bold")
plt.show()

输出:

matplotlib.axes.axes.twiny()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

matplotlib.axes