我正在学习一本有关 matplotlib 的电子书,并且我对一个有关双轴的示例有疑问。 代码是这样的:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0., np.e, 0.01)
y1 = np.exp(-x)
y2 = np.log(x)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y1)
ax1.set_ylabel('Y values for exp(-x)')
ax2 = ax1.twinx() # this is the important function
ax2.plot(x, y2, 'r')
ax2.set_xlim([0, np.e])
ax2.set_ylabel('Y values for ln(x)')
ax2.set_xlabel('Same X for both exp(-x) and ln(x)')
plt.show()
结果是这样的,x标签似乎丢失了:
我在 Pycharm 中运行代码。 标签缺失有什么原因吗?
谢谢
最佳答案
我不知道为什么,但当我把它放入 ax1 时,它就出现了。
ax1.set_xlabel('Same X for both exp(-x) and ln(x)')
关于python - 双轴中缺少标签(matplotlib),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23702364/