JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Generating an inverse colormap in Python Matplotlib

Author:JIYIK Last Updated:2025/05/04 Views:

A colormap is a simple way to map data values ​​to colors. Inverting a colormap means reversing the colormap for each value. Suppose we have a colormap where lower values ​​are mapped to yellow and higher values ​​are mapped to red. By inverting colormap, lower values ​​are now mapped to red and higher values ​​are mapped to yellow. This tutorial explains various ways to invert a colormap in Python Matplotlib.


Colormaps in Python Matplotlib

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(9)
y = [9, 2, 8, 4, 5, 7, 6, 8, 7]

plt.scatter(x, y, c=y, cmap="viridis")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Scatter Plot with Virdis colormap")
plt.colorbar()
plt.show()

It creates a scatter plot with its y values ​​mapped to viridisthe colormap. This means that points with low y values ​​will appear dark blue, points with high y values ​​will appear yellow, and points in between will appear blue and green.

We can also see a colorbar on the right side of the plot, which shows the color mapping for different values ​​of y.


_rInverse color mapping in Matplotlib Python using

In Matplotlib, we can _rinvert a colormap by adding at the end of the colormap name, for example cmap='viridis_r'will simply invert viridisthe colormap.

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(9)
y = [9, 2, 8, 4, 5, 7, 6, 8, 7]

plt.scatter(x, y, c=y, cmap="viridis_r")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Scatter Plot with Inverse Virdis colormap")
plt.colorbar()
plt.show()

It creates a scatter plot with the y values ​​mapped to the inverted viridiscolormap, so that the higher value points in the plot get dark blue and the lower value points get yellow.

We can also see that in this example the colorbar on the right has inverted colors.


matplotlib. colors.Colormap.reversed()Invert Colormap in Matplotlib Python

Another way to invert a Colormap in Python is to use matplotlib.color.Colormap.reversed()the method to create an inverted Colormap.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

x = np.arange(9)
y = [9, 2, 8, 4, 5, 7, 6, 8, 7]

initial_cmap = cm.get_cmap("viridis")
reversed_cmap = initial_cmap.reversed()

plt.scatter(x, y, c=y, cmap=reversed_cmap)
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Scatter Plot with Inverse Virdis colormap")
plt.colorbar()
plt.show()

It also creates a scatter plot with the y values ​​mapped to the inverted viridiscolormap. reversed()The method inverts the colormap object to which it is applied.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap

x = np.arange(9)
y = [9, 2, 8, 4, 5, 7, 6, 8, 7]

initial_cmap = cm.get_cmap("viridis")
reversed_cmap = ListedColormap(initial_cmap.colors[::-1])

plt.scatter(x, y, c=y, cmap=reversed_cmap)
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Scatter Plot with Inverse Virdis colormap")
plt.colorbar()
plt.show()

It also creates a scatter plot with the y values ​​mapped to the inverted viridiscolormap. Any colormap is just a list of colors. We can cmap. colorsaccess the color list of a colormap using cmap. We then invert the list and finally convert the inverted list back to a colormap using the function matplotlib.colsfrom the package .ListedColormap()

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Enumerating a dictionary in Python

Publish Date:2025/05/05 Views:98 Category:Python

The function in Python enumerate() returns an object of enumeration type and adds a counter variable to iterate over a list or other type of collection. It makes looping over such objects easier. When we pass an enumeration object to list()

Changing dictionary values in Python

Publish Date:2025/05/05 Views:108 Category:Python

This tutorial will discuss various ways to change the value of a particular key in Python dictionary. We can do this by using the following methods. dict.update() method for cycle. Dictionary Unpacking dict.update() How to change dictionary

Finding the maximum value in a Python dictionary

Publish Date:2025/05/05 Views:60 Category:Python

This tutorial explains how to get a key with the maximum value in Python. Since the method has changed from the previous Python versions, it also lists some sample codes to clarify the concepts. Use operator.itemgetter() the method to get t

How to read input from stdin in Python

Publish Date:2025/05/05 Views:124 Category:Python

This tutorial discussed stdin the methods of reading input from in Python. You can read directly from the console or from a file name specified in the console. In Python, fileinput.input() use stdin fileinput We can use the read module in P

Maximum integer in Python

Publish Date:2025/05/05 Views:55 Category:Python

This tutorial will discuss the maximum integer value in different versions of Python and how we can get it. In Python 2, integers and long integers are different data types. The maximum value of an integer is 2 31 -1. If the value exceeds t

Get a list of time zones using Python

Publish Date:2025/05/05 Views:107 Category:Python

When developing real-world applications, software developers must ensure that the application can support users from both their own country and other parts of the world. Since most countries have different time zones and many people around

Convert NumPy array to list in Python

Publish Date:2025/05/05 Views:101 Category:Python

Lists and arrays are the two most basic and commonly used collection objects in Python. They are both mutable and are used to store a collection of elements under a common name and each element has a specific location that can be used to ac

Appending 2D Arrays in Python

Publish Date:2025/05/05 Views:64 Category:Python

In Python, we can have ND arrays. We can use NumPy module to process arrays in Python. This tutorial demonstrated the different methods you can use to append values ​​to a two-dimensional array in Python. Use append() the function to ap

Sliding average of NumPy arrays in Python

Publish Date:2025/05/05 Views:190 Category:Python

The sliding average is often used to study time series data by calculating the average of data at a specific time interval. It is used to eliminate some short-term fluctuations and study data trends. When studying stock price trends, the si

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial