Thursday, 21 September 2023

How to Set Labels for the X and Y Axes in Pyplot?

Using xlabel, ylabel methods of plot, we can set the labels for x and y axis.

 

Example

plt.xlabel('Year')

plt.ylabel('Population in Millions')

 

plot_set_axis_labels.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 in millions by year wise', verticalalignment='center',
          horizontalalignment='center',  color='blue', fontweight='bold',
          backgroundcolor='lightblue')

# Display the plot
plt.show()

 

Output


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment