Python中的 Matplotlib.axes.Axes.secondary_xaxis()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.secondary_xaxis()函数
matplotlib 库的轴模块中的Axes.secondary_xaxis()函数也用于向该轴添加第二个 x 轴。
Syntax: Axes.secondary_xaxis(self, location, *, functions=None, **kwargs)
Parameters: This method accept the following parameters that are described below:
- location : This parameter is the position to put the secondary axis.
- functions : This parameter is used to specify the transform function and its inverse.
Returns: This method returns the the following:
- ax : This return the axes._secondary_axes.SecondaryAxis.
注意:此函数适用于 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_xaxis('top')
secax.set_xlabel('Secondary-X-Axis')
ax.set_title('matplotlib.axes.Axes.secondary_xaxis() 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(x, y)
ax.set_xlabel('Degree')
ax.set_ylabel('Frequency')
def val1(x):
return x * np.pi / 180
def val2(x):
return x * 180 / np.pi
secax = ax.secondary_xaxis('top', functions =(val1, val2))
secax.set_xlabel('Radian')
ax.set_title('matplotlib.axes.Axes.secondary_xaxis() Example',
fontsize = 14, fontweight ='bold')
plt.show()
输出: