
- 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 do I format the axis number format to thousands with a comma in Matplotlib?
First, we can make two lists of x and y, where the values will be more than 1000. Then, we can use the ax.yaxis.set_major_formatter method where can pass StrMethodFormatter('{x:,}') method with {x:,} formatter that helps to separate out the 1000 figures from the given set of numbers.
Steps
Make two lists having numbers greater than 2000.
Create fig and ax variables using subplots method, where default nrows and ncols are 1, using subplot() method.
Plot line using x and y (from step 1).
Set the formatter of the major ticker, using ax.yaxis.set_major_formatter() method, where StrMethodFormatter helps to make 1000 with common, i.e., expression {x:,}. Use a new-style format string (as used by 'str.format') to format the tick.
Set the formatter of the major ticker, using ax.xaxis.set_major_formatter() method, where StrMethodFormatter helps to make 1000 with common, i.e., expression {x:,}. Use a new-style format string (as used by 'str.format') to format the tick.
To show the figure, use plt.show() method.
Example
import matplotlib.pyplot as plt from matplotlib.ticker import FormatStrFormatter, StrMethodFormatter x = [10110, 20110, 40110, 6700] y = [20110, 10110, 30110, 9700] fig, ax = plt.subplots() ax.plot(x, y) ax.yaxis.set_major_formatter(StrMethodFormatter('{x:,}')) ax.xaxis.set_major_formatter(StrMethodFormatter('{x:,}')) plt.show()
Output
- Related Questions & Answers
- Format amount values for thousands number with two decimal places in MySQL?
- How do I format a number as decimal to store it in MySQL?
- How do I re-format datetime in MySQL?
- How do I format a string using a dictionary in Python 3?
- How do I change the range of the X-axis with datetimes in Matplotlib?
- How do I display the content of a JavaScript object in a string format?
- How to format a number with two decimals in JavaScript?
- How to format number in JSP?
- How can I format a float using matplotlib's LaTeX formatter?
- How to format hexadecimal Number in JavaScript?
- Extract Matplotlib colormap in hex-format
- How do I get an ISO 8601 date in string format in Python?
- How to format Java LocalDateTime as ISO_DATE_TIME format
- How do I print a Celsius symbol with Matplotlib?
- Precision on a number format in Java