Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Pandas
  • Numpy
  • Seaborn
  • Ploty
  • Data visualization
  • Data Analysis
  • Power BI
  • Tableau
  • Machine Learning
  • Deep Learning
  • NLP
  • Computer Vision
  • Data Science for Beginners
  • Data Science interview questions
  • Data analysis interview questions
  • NLP Interview questions
Open In App
Next Article:
How to change axes limits in matplotlib?
Next article icon

How to change colorbar labels in matplotlib ?

Last Updated : 29 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to see how to change color bar labels in matplotlib using Python.

The colorbar() function is used to plot the color bar which belongs to the pyplot module of matplotlib adds a colorbar to a plot indicating the color scale.

Syntax: matplotlib.pyplot.colorbar(mappable=None, cax=None, ax=None, **kwarg)

Parameters:

  • ax: This parameter is an optional parameter and it contains Axes or list of Axes.
  • **kwarg(keyword arguments): This parameter is an optional parameter and are of two kinds:
  • colorbar properties:
  • extend:{‘neither’, ‘both’, ‘min’, ‘max’} makes pointed end(s) for out-of-range values.
  • label:The label on the colorbar’s long axis.
  • ticks:None or list of ticks or Locator.

Returns:colorbar which is an instance of the class ‘matplotlib.colorbar.Colorbar’.     

Create a simple colorbar for demonstration

To create the colorbar we will use color() methods, for this, we will create the dataset and then use a scatterplot for demonstration.

Python3
# Python Program illustrating # pyplot.colorbar() method import numpy as np import matplotlib.pyplot as plt  # Dataset # List of total number of items purchased # from each products purchaseCount = [100, 200, 150, 23, 30, 50,                 156, 32, 67, 89]  # List of total likes of 10 products likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]  # List of Like/Dislike ratio of 10 products ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,         1.28, 1.09, 1.02]  # scatterplot plt.scatter(x=purchaseCount, y=likes, c=ratio, cmap="summer")  plt.colorbar() plt.show() 

Output:

Method 1: Change labels font size in a color label

To change the label's font size we will use ax.tick_params() methods which increase the font of the labels.

Python3
# Python Program illustrating # pyplot.colorbar() method import numpy as np import matplotlib.pyplot as plt  # Dataset # List of total number of items purchased # from each products purchaseCount = [100, 200, 150, 23, 30, 50,                 156, 32, 67, 89]  # List of total likes of 10 products likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]  # List of Like/Dislike ratio of 10 products ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,         1.28, 1.09, 1.02]  # scatterplot plt.scatter(x=purchaseCount, y=likes, c=ratio,             cmap="summer")  plt.colorbar().ax.tick_params(labelsize=10)  plt.show() 

Output:

Method 2: Change and Rotate the position of the label

To rotate the colorbar labels we will use set_xticklabels() and set_yticklabels() methods for horizontal and vertical.

Example 1: Changing position horizontally

Python3
# Python Program illustrating # pyplot.colorbar() method import numpy as np import matplotlib.pyplot as plt  # Dataset # List of total number of items purchased # from each products purchaseCount = [100, 200, 150, 23, 30, 50,                 156, 32, 67, 89]  # List of total likes of 10 products likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]  # List of Like/Dislike ratio of 10 products ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,         1.28, 1.09, 1.02]  # scatterplot plt.scatter(x=purchaseCount, y=likes, c=ratio,             cmap="summer")  plt.colorbar().ax.set_ylabel('Label',                              rotation=120)  plt.show() 

Output:

Example 2: Changing position vertically

Python3
# Python Program illustrating # pyplot.colorbar() method import numpy as np import matplotlib.pyplot as plt  # Dataset # List of total number of items purchased # from each products purchaseCount = [100, 200, 150, 23, 30, 50,                 156, 32, 67, 89]  # List of total likes of 10 products likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]  # List of Like/Dislike ratio of 10 products ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,         1.28, 1.09, 1.02]  # scatterplot plt.scatter(x=purchaseCount, y=likes, c=ratio, cmap="summer") plt.colorbar(orientation="horizontal").ax.set_xticklabels(ratio, rotation=45)  plt.show() 

Output:


Next Article
How to change axes limits in matplotlib?
author
kumar_satyam
Improve
Article Tags :
  • Python
  • Python-matplotlib
Practice Tags :
  • python

Similar Reads

  • How to Change Line Color in Matplotlib?
    Matlab's plotting functions are included in Python by the Inclusion of the library Matplotlib. The library allows the plotting of the data of various dimensions without ambiguity in a plot. The library is widely used in Data Science and Data visualization. In this article, we will discuss how to cha
    3 min read
  • Adding Labels to Histogram Bars in Matplotlib
    In Matplotlib, a histogram displays data distribution across defined intervals, known as bins. Each bar in the histogram represents the frequency or density of data points within these bins. To communicate these values, you can add labels to the bars by placing text directly on or above them, showin
    3 min read
  • How to Change Fonts in matplotlib?
    Prerequisites:  Matplotlib  In this article, we will see how can we can change the font family of our graph using matplotlib. A variety of fonts are in fact supported by matplotlib, alone has to do in order to implement is pass the name as value to fontname parameter. Approach: Import required libra
    2 min read
  • How to Change the Font Size of Colorbars in Matplotlib
    Matplotlib is a powerful and widely used library in Python for data visualization. It offers a variety of plotting functions to create complex graphs and charts. One common visualization is a heatmap, which often includes a color bar to indicate the scale of values represented by colors. Adjusting t
    4 min read
  • How to change axes limits in matplotlib?
    Sometimes, when you create a plot default axes X and Y may not show the full picture you want. Changing the axes limits helps you focus on specific data ranges or improve how your plot looks. There are two methods available in Axes module tochange the limits: matplotlib.axes.Axes.set_xlim(): It is u
    2 min read
  • Rotation of colorbar tick labels in Matplotlib
    Colorbar is an axis that indicates the mapping of data values to the colors used in plot. The colorbar() function in pyplot module of matplotlib adds a colorbar to a plot indicating the color scale. Sometimes it is desirable to rotate the ticklabels for better visualization and understanding. To cha
    3 min read
  • How to change the size of axis labels in Matplotlib?
    Matplotlib is a Python library that helps in visualizing and customizing various plots. One of the customization you can do is to change the size of the axis labels to make reading easier. In this guide, we’ll look how to adjust font size of axis labels using Matplotlib. Let’s start with a basic plo
    2 min read
  • Animating the Colorbar in Matplotlib
    Animating visual elements in data plots can significantly enhance the interpretability and appeal of data presentations. One such element is the colorbar, which provides a visual representation of the data range and can be particularly useful in heatmaps and contour plots. In this article, we will e
    4 min read
  • Matplotlib.colors.from_levels_and_colors() in Python
    Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.colors.from_levels_and_colors() The matplotlib.colors.from_levels_and_colors(
    2 min read
  • Matplotlib.axes.Axes.get_label() in Python
    Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences