A line plot is often used to see the relationship between two variables x and y. We can use the matplotlib.plot() function to plot a line plot using the matplotlib Python library.
Let’s look at an example. Let’s read the flights dataset. The dataset contains various information, such as years, number of passengers in those years, etc. Let’s say we want to plot a line plot between years and the number of passengers and see how these variables are related.
We can use the following Python code to plot a line plot using the matplotlib Python library.
import pandas from matplotlib import pyplot df = pandas.read_csv("flights.csv") pyplot.plot(df["year"], df["passengers"]) pyplot.xlabel("Year") pyplot.ylabel("Number of Passengers") pyplot.savefig("line-plot-matplotlib.png") pyplot.close()
Here, we are using the pyplot.plot() function to plot the line graph. The first parameter indicates the x values and the second parameter indicates the y values. The pyplot.xlabel() and pyplot.ylabel() functions are used to label the x-axis and the y-axis, respectively.
The output line graph will look like the following:






0 Comments