Monday 21 August 2023

Pyplot: Quick introduction to line graph

plt.plot() is a core method used to create various types of plots in Matplotlib. It is particularly used to create line plots.

 

Example

plt.plot(x, y)

 

In this above example, x and y specify python lists contain the data points to be plotted. The plt.plot() method takes x and y lists as arguments and connects the points with straight lines. Element of list 'x' are used in X-Axis, whereas element of list 'y' are used in Y-Axis.

 

Once you define the plot, you should use plt.show method to display the plot on the screen.

 

line_graph.py

import matplotlib.pyplot as plt

# Sample data
year = [2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022]
population = [1246.2, 1262.6, 1279.7, 1297.4, 1315.8, 1334.9, 1354.7, 1374.2, 1407.6, 1417.2]

# Create a line plot
plt.plot(year, population)

# Add labels and title
plt.xlabel('Year')
plt.ylabel('Population in Millions')
plt.title('India Population')

# Display the plot
plt.show()

 

Output



 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment