Matplotlib 为多轴图发出 x 和 y 标签
Matplotlib issue x and y label for multi axes figure
import matplotlib
import matplotlib.pyplot as plt
import numpy as nm
x = nm.linspace(start=0,stop=20,num=30)
fig=plt.figure()
ax1 = fig.add_axes([0,0.6,0.6,0.4])
ax2 = fig.add_axes([0,0,0.8,0.4])
ax1.plot(x,nm.sin(x))
ax1.set_xlabel('x',fontsize=15,color='r')
ax1.set_ylabel('sin(x)',fontsize=15,color='r')
ax2.plot(x,nm.cos(x))
ax2.set_xlabel('x',fontsize=15,color='r')
ax2.set_ylabel('cos(x)',fontsize=15,color='r')
plt.show()
输出我看不到 ax2 的 xlabel,也看不到 ax1 和 ax2 的 y 标签。图片如下所示:
enter code here
enter image description here
这是预期的,因为您要求使用 fig.add_axes([0,...])
创建与图的左边缘对齐的轴。底轴也是如此,您已使用 fig.add_axes([0,0,...])
.
将其与图的左下角对齐
增加第一个值,例如fig.add_axes([0.125,...])
为轴左侧或底部的轴装饰留出空间。
一般推荐使用subplots函数(如add_subplot, plt.subplots or GridSpec),这样这些细节会自动处理。
import matplotlib
import matplotlib.pyplot as plt
import numpy as nm
x = nm.linspace(start=0,stop=20,num=30)
fig=plt.figure()
ax1 = fig.add_axes([0,0.6,0.6,0.4])
ax2 = fig.add_axes([0,0,0.8,0.4])
ax1.plot(x,nm.sin(x))
ax1.set_xlabel('x',fontsize=15,color='r')
ax1.set_ylabel('sin(x)',fontsize=15,color='r')
ax2.plot(x,nm.cos(x))
ax2.set_xlabel('x',fontsize=15,color='r')
ax2.set_ylabel('cos(x)',fontsize=15,color='r')
plt.show()
输出我看不到 ax2 的 xlabel,也看不到 ax1 和 ax2 的 y 标签。图片如下所示:
enter code here
enter image description here
这是预期的,因为您要求使用 fig.add_axes([0,...])
创建与图的左边缘对齐的轴。底轴也是如此,您已使用 fig.add_axes([0,0,...])
.
增加第一个值,例如fig.add_axes([0.125,...])
为轴左侧或底部的轴装饰留出空间。
一般推荐使用subplots函数(如add_subplot, plt.subplots or GridSpec),这样这些细节会自动处理。