In this Python Matplotlib tutorial we want to learn about Python Matplotlib Line Plot, so Data visualization plays an important role in understanding complex information, also Python offers different libraries for data visualization, one of them are Matplotlib, Matplotlib is popular data visualization library in Python. in this article we want to learn about plotting line chart with Matplotlib, basically line chart enables us to represent trends and patterns in our data with easy.

First of all we need to install matplotlib and we can use pip for that.

1 |
pip install matplotlib |

This is the complete code for this article

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import matplotlib.pyplot as plt # Prepare the Data years = [2010, 2011, 2012, 2013, 2014, 2015] population = [800, 900, 1100, 1300, 1500, 1700] # Create the Line Plot plt.plot(years, population) # Customize the Plot plt.title("Population Growth") plt.xlabel("Year") plt.ylabel("Population") plt.grid(True) # Display or Save the Plot plt.show() |

First of all we need to import required libraries from Matplotlib.

1 |
import matplotlib.pyplot as plt |

To illustrate line plotting, let’s consider a simple example with some data. Assume we have a list of years and the corresponding population values.

1 2 |
years = [2010, 2011, 2012, 2013, 2014, 2015] population = [800, 900, 1100, 1300, 1500, 1700] |

With the data ready, we can proceed to create the line plot using the plt.plot() function. This function takes two parameters: the x-values and the y-values.

1 |
plt.plot(years, population) |

Matplotlib provides different customization options to enhance the appearance of our line plot. We can add labels, titles, gridlines, legends and many more.

1 2 3 4 |
plt.title("Population Growth") plt.xlabel("Year") plt.ylabel("Population") plt.grid(True) |

After that we have customized our line plot, we can choose to either display it or save it as an image. To display the plot, use the plt.show() function.

1 |
plt.show() |

If you wish to save the line plot as an image, you can use the plt.savefig() function instead.

1 |
plt.savefig("line_plot.png") |

Run the complete code and this will be the result