matplotlib How to set X and Y axis Title

wnavrhmk  于 2023-02-23  发布在  其他
关注(0)|答案(2)|浏览(188)

I have imported:

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

to show:

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

I have tried plt.xlabel('X axis title') and plt.ylabel('Y axis title ) and several other codes but none are working.
I'm just trying to label the x, y axis.

vkc1a9a2

vkc1a9a21#

See let us consider that your dataset to make the graph can be divided into 4 parts,train_losses_x,train_losses_y,test_losses_x and test_losses_y.So it can be used as following

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

I hope this help you.

bis0qfac

bis0qfac2#

您可以简单地使用xlabel和ylabel函数来打印字符串。

import matplotlib.pyplot as plt
time = [2001,2002, 2003, 2004, 2005, 2006]
population = ["100Lakh","200Lakh", "300Lakh", "400Lakh", "500Lakh", "600Lakh"]
# x = time, y = population
plt.plot(time, population)
plt.xlabel("time")
plt.ylabel("Popluation of city")
plt.show()

相关问题