Python axhline、标题和坐标轴标签

2 投票
1 回答
18130 浏览
提问于 2025-04-18 15:23

我正在尝试用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()

其他的都正常显示,就是没有标题和坐标轴的标签。图例是有的。

1 个回答

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()

撰写回答