matplotlib.pyplot.clabel() in Python Last Updated : 29 Aug, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Contour plots or Level plots are a way to display a three-dimensional surface on a two-dimensional plane. It graphs as contours as one output variable z and two predictor variables x and y on the y-axis. Often such contours are also known as z-slices.The clabel() method in mathplotlib.pyplot is used add labels to line contours in instances of the classes to support contour plotting. Syntax: matplotlib.pyplot.clabel(CS, levels=None, **kwargs) Parameters: CS: The ContourSet to label.levels: A list of level values, that should be labeled. The list must be a subset of CS.levels. If not given,all levels are labeled. It is an optional argument(default value is None).fontsize: Size in points or relative size e.g., 'smaller', 'x-large'. See Text.set_size for accepted string values.colors: The label colors- If None, the color of each label matches the color of the corresponding contour.If one string color, e.g., colors = 'r' or colors = 'red', all labels will be plotted in this color.If a tuple of matplotlib color args (string, float, rgb, etc), different labels will be plotted in different colors in the order specified. Below are some programs to illustrate the use of matplotlib.pyplot.clabel() : Example 1: Create a simple contour plot with labels using default colors. The inline argument to clabel will control whether the labels are draw over the line segments of the contour, removing the lines beneath the label. Python3 # importing the required libraries import numpy import matplotlib.pyplot # creating the graph delta = 0.025 x = numpy.arange(-3.0, 3.0, delta) y = numpy.arange(-2.0, 2.0, delta) X, Y = numpy.meshgrid(x, y) Z1 = numpy.exp(-X**2 - Y**2) Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2) Z = (Z1 - Z2) * 2 # adding labels to the line contours fig, ax = matplotlib.pyplot.subplots() CS = ax.contour(X, Y, Z) ax.clabel(CS, inline=1, fontsize=10) ax.set_title('Simplest default with labels') Output: Example 2: Contour labels can be placed manually by providing a list of positions (in data coordinate). See ginput_manual_clabel.py for interactive placement. Python3 # importing the required libraries import numpy import matplotlib.pyplot # creating the graph delta = 0.025 x = numpy.arange(-3.0, 3.0, delta) y = numpy.arange(-2.0, 2.0, delta) X, Y = numpy.meshgrid(x, y) Z1 = numpy.exp(-X**2 - Y**2) Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2) Z = (Z1 - Z2) * 2 # adding labels to the line contours fig, ax = matplotlib.pyplot.subplots() CS = ax.contour(X, Y, Z) manual_locations = [(-1, -1.4), (-0.62, -0.7), (-2, 0.5), (1.7, 1.2), (2.0, 1.4), (2.4, 1.7)] ax.clabel(CS, inline=1, fontsize=10, manual=manual_locations) ax.set_title('labels at selected locations') Output: Example 3: You can force all the contours to be the same color. Python3 # importing the required libraries import numpy import matplotlib.pyplot # creating the graph delta = 0.025 x = numpy.arange(-3.0, 3.0, delta) y = numpy.arange(-2.0, 2.0, delta) X, Y = numpy.meshgrid(x, y) Z1 = numpy.exp(-X**2 - Y**2) Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2) Z = (Z1 - Z2) * 2 # adding labels to the line contours fig, ax = matplotlib.pyplot.subplots() CS = ax.contour(X, Y, Z, 6, colors='k', ) ax.clabel(CS, fontsize=9, inline=1) ax.set_title('Single color - negative contours dashed') Output: Example 4: You can set negative contours to be solid instead of dashed: Python3 # importing the required libraries import numpy import matplotlib.pyplot # creating the graph delta = 0.025 x = numpy.arange(-3.0, 3.0, delta) y = numpy.arange(-2.0, 2.0, delta) X, Y = numpy.meshgrid(x, y) Z1 = numpy.exp(-X**2 - Y**2) Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2) Z = (Z1 - Z2) * 2 # adding labels to the line contours matplotlib.rcParams['contour.negative_linestyle'] = 'solid' fig, ax = matplotlib.pyplot.subplots() CS = ax.contour(X, Y, Z, 6, colors='k', ) ax.clabel(CS, fontsize=9, inline=1) ax.set_title('Single color - negative contours solid') Output: Example 5: You can manually specify the colors of the contour. Python3 # importing the required libraries import numpy import matplotlib.pyplot # creating the graph delta = 0.025 x = numpy.arange(-3.0, 3.0, delta) y = numpy.arange(-2.0, 2.0, delta) X, Y = numpy.meshgrid(x, y) Z1 = numpy.exp(-X**2 - Y**2) Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2) Z = (Z1 - Z2) * 2 # adding labels to the line contours fig, ax = matplotlib.pyplot.subplots() CS = ax.contour(X, Y, Z, 6, linewidths=np.arange(.5, 4, .5), colors=('r', 'green', 'blue', (1, 1, 0), '#afeeee', '0.5') ) ax.clabel(CS, fontsize=9, inline=1) ax.set_title('Crazy lines') Output: Comment More infoAdvertise with us Next Article matplotlib.pyplot.clabel() in Python R riturajsaha Follow Improve Article Tags : Python Python-matplotlib Matplotlib Pyplot-class Practice Tags : python Similar Reads Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is 8 min read Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam 3 min read Like