- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get a matplotlib Axes instance to plot to?
To get the axes instance, we will use the subplots() method.
Steps
Make a list of years.
Make a list of populations in that year.
Get the number of labels using np.arrange(len(years)) method.
Set the width of the bars.
Create fig and ax variables using subplots() method, where default nrows and ncols are 1.
Set the Y-axis label of the figure using set_ylabel().
Set the title of the figure, using set_title() method.
Set the x-ticks with x that is created in step 3, using set_xticks method.
Set the xtick_labels with years data, using set_xticklabels method.
Use plt.show() method to show the figure.
Example
from matplotlib import pyplot as plt import numpy as np years = [1901, 1911, 1921, 1931, 1941, 1951, 1961, 1971, 1981, 1991, 2001, 2011] population = [237.4, 238.4, 252.09, 251.31, 278.98, 318.66, 361.09, 439.23, 548.16, 683.33, 846.42, 1028.74] x = np.arange(len(years)) # the label locations width = 0.35 # the width of the bars fig, ax = plt.subplots() # axes instance ax.set_ylabel('Population(in million)') ax.set_title('Years') ax.set_xticks(x) ax.set_xticklabels(years) plt.show()
Output
- Related Articles
- How to plot a point on 3D axes in Matplotlib?
- How to plot sine curve on polar axes using Matplotlib?
- How to get a Gantt plot using matplotlib?
- How to rotate a simple matplotlib Axes?
- How to plot single data with two Y-axes (two units) in Matplotlib?
- How to hide axes but keep axis-labels in 3D Plot with Matplotlib?
- How to switch axes in Matplotlib?
- How to get all the legends from a plot in Matplotlib?
- Plot 3D bars without axes in Matplotlib
- How to make axes transparent in Matplotlib?
- How to a plot stem plot in Matplotlib Python?
- How to change axes background color in Matplotlib?
- How to hide axes and gridlines in Matplotlib?
- Plot a vector field over the axes in Python Matplotlib?
- How to plot matplotlib contour?

Advertisements