【问题标题】:How to set X and Y axis Title in matplotlib.pyplot如何在 matplotlib.pyplot 中设置 X 和 Y 轴标题
【发布时间】:2019-10-20 04:08:31
【问题描述】:

我已经导入了:

%matplotlib inline %config InlineBackend.fugure_format = 'retina' import matplotlib.pyplot as plt

显示:

plt.plot(train_losses, label='Training loss') plt.plot(test_losses, label='Test/Validation loss') plt.legend(frameon=False)

我尝试了plt.xlabel('X axis title') 和plt.ylabel('Y axis title) 以及其他几个代码,但没有一个有效。

我只是想标记 x、y 轴。

【问题讨论】:

  • plt.show() 是否显示它们?
  • plt.xlabel('X axis title') 将设置 x 标签。因此,如果这对您不起作用,请提供 minimal reproducible example 并使用您正在使用的版本更新问题。
  • @ImportanceOfBeingErnest 我不知道如何重现这段代码,它是 DL 神经网络结果,.. 我想你可以使用 x 轴 x = 1, 2, 3, 4, 5 和 y = 5,4,3,2,1 并只做两行使用我导入的模块在图中..
  • @YaakovBressler 不,它不起作用,当我使用 plt.xlabel('X axis title') 时出现错误 'str' object is not callable
  • 是的,应该独立于实际数据。当然,如果我使用一些通用列表,如 1、2、3 等,我会得到正确的结果。所以你需要让它可重现。

标签: python matplotlib plot graph


【解决方案1】:

让我们考虑一下,您制作图表的数据集可以分为 4 个部分,train_losses_x、train_losses_y、test_losses_x 和 test_losses_y。所以它可以如下使用

fig,ax=plt.subplot()
train_line=ax.plot(train_losses_x,train_losses_y,label="Training Loss")
test_line=ax.plot(test_losses_x,test_losses_y,label="Test/Validation Loss")
ax.set_xlabel("X_axis_title")
ax.set_ylabel("Y_axis_title")
legend = ax.legend(loc='upper right')
plt.show()

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    相关资源
    最近更新 更多