Python axhline、标题和轴标签

Han*_*nah 2 python matplotlib title axis-labels

我正在尝试使用 matplotlib 在另一个图上绘制水平线。除了标题和轴标签从不显示之外,一切正常。这是如何运作的?

*编辑-抱歉,代码看起来有点像这样: from matplotlib import pyplot as plt n=100

plt.axhline(y=n, label='Old')
plt.plot([5, 6, 7, 8], [100, 110, 115, 150], 'ro', label='New')
plt.xlabel=('Example x')
plt.ylabel=('Example y')
plt.title=('Example Title')
plt.legend()
plt.axis([0,10,50,150])
plt.show()
Run Code Online (Sandbox Code Playgroud)

一切正常显示,只是没有标题和轴标签。传说就在那里。

GCi*_*ien 5

尝试这个:

fig = plt.figure()

ax = fig.add_subplot(111)
ax.axhline(y=n, label='Old')
ax.plot([5, 6, 7, 8], [100, 110, 115, 150], 'ro', label='New')

ax.set_xlabel('Example x')
ax.set_ylabel('Example y')
ax.set_title('Example Title')

ax.legend()
ax.set_xticks([0,10,50,150])
ax.set_yticks([0,10,50,150])

plt.show()
Run Code Online (Sandbox Code Playgroud)

  • +1 使用 OO 界面!我通常使用“fig, ax = plt.subplots(1, 1)”。 (3认同)