

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 insert a scale bar in a map in Matplotlib?
To insert a scale bar in a map in matplotlib, we can use AnchoredBar() class to instantiate the scalebar object.
Steps
Set the figure size and adjust the padding between and around the subplots.
Create random data using numpy.
Use imshow() method to display data as an image, i.e., on a 2D regular raster.
Get the current axis using gca() method.
Draw a horizontal scale bar with a center-aligned label underneath.
Add a scalebar artist to the current axis.
Turn off the axes.
To display the figure, use show() mthod.
Example
from matplotlib import pyplot as plt from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(5, 5) img = plt.imshow(data, cmap="YlGnBu") ax = plt.gca() scalebar = AnchoredSizeBar(ax.transData, 1, "1 Meter", 9) ax.add_artist(scalebar) ax.axis('off') plt.show()
Output
- Related Questions & Answers
- How to plot a density map in Python Matplotlib?
- How to plot Time Zones in a map in Matplotlib
- How can I convert numbers to a color scale in Matplotlib?
- How to create a legend for a 3D bar in Matplotlib?
- How to mark a specific level in a contour map on Matplotlib?
- How to display percentage above a bar chart in Matplotlib?
- How to make a broken horizontal bar plot in Matplotlib?
- How to Create a Diverging Stacked Bar Chart in Matplotlib?
- How to plot a bar graph in Matplotlib from a Pandas series?
- How to plot a bar chart for a list in Python matplotlib?
- Dynamically updating a bar plot in Matplotlib
- How to plot a 3D density map in Python with Matplotlib?
- map insert() in C++ STL
- How to create a Matplotlib bar chart with a threshold line?
- How to plot a Bar Chart with multiple labels in Matplotlib?
Advertisements