Matplotlib是Python中的一个库,它是数字的-NumPy库的数学扩展。轴类包含大多数图形元素:Axis,Tick,Line2D,Text,Polygon等,并设置坐标系。 Axes实例通过callbacks属性支持回调。
matplotlib.axes.Axes.get_shared_x_axes()函数
matplotlib库的axiss模块中的Axes.get_shared_x_axes()函数用于返回对x轴的共享轴Grouper对象的引用。
用法:
Axes.get_shared_x_axes(self)
以下示例说明了matplotlib.axes中的matplotlib.axes.Axes.get_shared_x_axes()函数:
范例1:
# 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(-40, 120, 100))
ax1.set_xlim(0, 100)
ax1.set_ylabel('Fahrenheit')
ax_twin .set_ylabel('Celsius')
w = ax1.get_shared_x_axes()
w1 = ax_twin.get_shared_x_axes()
for i in w:
x, y = list(i)
ax1.text(40, 100, "Value return:\n",
fontweight ="bold")
ax1.text(27, 90, str(x)+"\n"+str(y),
fontweight ="bold")
fig.suptitle('matplotlib.axes.Axes.get_shared_x_axes()\
function Example\n\n', fontweight ="bold")
plt.show()
输出:
范例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_xlabel('time (s)')
ax1.set_ylabel('exp', color = color)
ax1.plot(t, data1, color = color)
ax1.tick_params(axis ='y', labelcolor = color)
ax2 = ax1.twinx()
color = 'tab:green'
ax2.set_ylabel('sin', color = color)
ax2.plot(t, data2, color = color)
ax2.tick_params(axis ='y', labelcolor = color)
w = ax1.get_shared_x_axes()
w1 = ax2.get_shared_x_axes()
for i in w:
x, y = list(i)
ax1.text(4, 15000, "Value return:\n",
fontweight ="bold")
ax1.text(2, 13000, str(x)+"\n"+str(y),
fontweight ="bold")
fig.suptitle('matplotlib.axes.Axes.get_shared_x_axes()\
function Example\n\n', fontweight ="bold")
plt.show()
输出:
相关用法
注:本文由纯净天空筛选整理自SHUBHAMSINGH10大神的英文原创作品 Matplotlib.axes.Axes.get_shared_x_axes() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。