matplotlib cm
时间: 2023-09-08 13:16:31 浏览: 108
matplotlib.cm is a module in the matplotlib library that provides a collection of colormaps for visualizing data. A colormap is a mapping from a range of values to a range of colors. In matplotlib, colormaps are instances of the class "Colormap" and can be used to set the color of markers, lines, and other graphical elements.
Some of the commonly used colormaps in matplotlib.cm are:
- viridis: a perceptually uniform colormap that is widely used in scientific visualization.
- magma: a colormap that is similar to viridis but has a higher contrast.
- inferno: a colormap that is similar to magma but has a higher contrast in the yellow-to-red range.
- plasma: a colormap that has a more uniform brightness across all colors.
- jet: a colormap that was popular in earlier versions of matplotlib but is now considered to be problematic due to its uneven luminance distribution.
To use a colormap in matplotlib, you can call the "colormap" method on an axis object and pass in the name of the desired colormap. For example:
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y, color=plt.cm.viridis(0.5))
plt.show()
```
This code creates a simple sine wave and plots it with a color that is halfway between the minimum and maximum values of the "viridis" colormap.
阅读全文