python - 双轴中缺少标签(matplotlib)

标签 python 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标签似乎丢失了:

matplolib twin axes missing label

我在 Pycharm 中运行代码。 标签缺失有什么原因吗?

谢谢

最佳答案

我不知道为什么,但当我把它放入 ax1 时,它就出现了。

ax1.set_xlabel('Same X for both exp(-x) and ln(x)')

关于python - 双轴中缺少标签(matplotlib),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23702364/

相关文章:

python - 无法使用 BeautifulSoup、urllib、selenium 提取完整的 HTML

python - 根据数据点的数量在同一个坐标轴上绘制 swarmplot 或 boxplot

python - 正则表达式查找不起作用的空格

python - 操作系统错误: [Errno 26] Text file busy on vagrant with synced_folder

python - 面向对象的 matplotlib 使用神秘地生成没有 mustache 或中线的箱线图

python - 在 Ubuntu 上导入 matplotlib

python - 在 ctypes 结构中访问 np.array

python - 如何在AMQP的python客户端中使用listen on basic.return

python - matplotlib rc colorcycle参数与散点图

python-3.x - 无法从 kdeplot 读取数据