Matplotlib – Set X Label for Plot

To set X-Label for plot in matplotlib, call xlabel()function on matplotlib.pyplot object and pass the required label value as string.

The following code snippet shows how to set the X label for plot with the string “Sample X-Label”.

</>
Copy
import matplotlib.pyplot as plt

plt.xlabel('Sample X-Label')

Example

In this example, we will draw a plot, and set its x-label to “Sample X-Label”.

example.py

</>
Copy
import matplotlib.pyplot as plt

# x axis and y axis data
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
y = [5, 7, 8, 1, 4, 9, 6, 3, 5, 2, 1, 8]

plt.plot(x, y)

#set x-label for plot axes
plt.xlabel('Sample X-Label')

plt.show()

Output

Matplotlib - Set X Label for Plot Axes
X Label set to “Sample X-Label” for Plot

Conclusion

Concluding this Matplotlib Tutorial, we learned how to set X-Label for plot axes.