# 在 Python Matplotlib 中生成反向色彩图

## 在 Python Matplotlib 中的色彩图(Colormaps)

``````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()
``````

## 在 Matplotlib Python 中使用 `_r` 进行反向颜色映射

``````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()
``````

## 在 Matplotlib Python 中使用 `matplotlib. colors.Colormap.reversed()` 反转 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()
``````

``````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()
``````