📜  Python中的 Matplotlib.pyplot.twinx()

📅  最后修改于: 2022-05-13 01:55:21.725000             🧑  作者: Mango

Python中的 Matplotlib.pyplot.twinx()

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 PyplotMatplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。

示例代码

# sample code
import matplotlib.pyplot as plt 
    
plt.plot([1, 2, 3, 4], [16, 4, 1, 8]) 
plt.show() 

输出:

matplotlib.pyplot.twinx()函数

matplotlib 库的 pyplot 模块中的twinx()函数用于创建和返回共享 x 轴的第二个轴。

下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.twinx()函数:

示例 #1:

matplotlib.pyplot.twinx(ax=None)

输出:

示例 #2:

# 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.twinx()
   
ax1.callbacks.connect("ylim_changed", GFG2)
ax1.plot(np.linspace(0, 120, 100))
ax1.set_xlim(30, 100)
   
ax1.set_ylabel('Fahrenheit')
ax_twin .set_ylabel('Celsius')
   
fig.suptitle('matplotlib.pyplot.twinx() function \
Example\n\n', fontweight ="bold")
plt.show()

输出: