📌  相关文章
📜  Python中的 Matplotlib.axes.Axes.secondary_yaxis()

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

Python中的 Matplotlib.axes.Axes.secondary_yaxis()

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

matplotlib.axes.Axes.secondary_yaxis()函数

matplotlib 库的轴模块中的Axes.secondary_yaxis()函数也用于向该轴添加第二个 y 轴。

注意:此函数适用于 Matplotlib 版本 >= 3.1

下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.secondary_xaxis()函数:

示例 1:

# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
      
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
   
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
   
secax = ax.secondary_yaxis('right')
secax.set_ylabel('Secondary-Y-Axis')
ax.set_title('matplotlib.axes.Axes.secondary_yaxis() Example',
             fontsize = 14, fontweight ='bold')
plt.show()

输出:

示例 2:

# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
import datetime
import matplotlib.dates as mdates
from matplotlib.transforms import Transform
from matplotlib.ticker import (
    AutoLocator, AutoMinorLocator)
  
fig, ax = plt.subplots(constrained_layout = True)
x = np.arange(0, 500, 2)
y = np.sin(3 * x * np.pi / 180)
ax.plot(y, x)
ax.set_ylabel('Degree')
ax.set_xlabel('Frequency')
  
  
def val1(y):
    return y * np.pi / 180
  
  
def val2(y):
    return y * 180 / np.pi
  
secax = ax.secondary_yaxis('right', functions =(val1, val2))
secax.set_ylabel('Radian')
ax.set_title('matplotlib.axes.Axes.secondary_yaxis() Example',
             fontsize = 14, fontweight ='bold')
plt.show()

输出: