双轴缺少标签(matplotlib)
我正在学习一本关于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中运行的这个代码。有没有可能是导致标签缺失的原因呢?
谢谢!
1 个回答
4
我不知道为什么,但当我把它放在ax1里时,它就出现了。
ax1.set_xlabel('Same X for both exp(-x) and ln(x)')