Plots in
In matplotlib
, we use the pyplot.xlabel()
and pyplot.ylabel()
functions to label the x and y-axis of the plot, respectively.
Let’s create a plot and label its x and y-axes as follows:
import matplotlib.pyplot as plt import numpy as np x = np.array([1, 5]) y = np.array([10, 20]) plt.plot(x, y) # using the xlabel and ylabel functions plt.xlabel("x axis") plt.ylabel("y axis") plt.show()
pyplot
module from the matplot
library.numpy
module.numpy.array()
method to create an array variable x
.numpy.array()
method to create another array variable, y
.pyplot.plot()
method to plot y
against x
(that is, x
on the x-axis and y
on the y-axis).pyplot.xlabel()
method to label the x-axis of our plot.pyplot.ylabel()
method to label the y-axis of our plot.pyplot.show()
method to ask Python to show us our plot.In Matplotlib
, we use the pyplot.title()
method to give a title to a plot.
Let’s create a plot and give it a title, as follows:
import matplotlib.pyplot as plt import numpy as np x= np.array([1, 5]) y = np.array([10, 20]) plt.plot(x, y) # usig the title() method plt.title('Y against X') # using the xlabel() and ylabel() methods plt.xlabel("x axis") plt.ylabel("y axis") plt.show()
RELATED TAGS
CONTRIBUTOR
View all Courses