Home > Backend Development > Python Tutorial > Explore matplotlib color mapping: create gorgeous drawings

Explore matplotlib color mapping: create gorgeous drawings

WBOY
Release: 2024-01-10 16:35:31
Original
975 people have browsed it

Explore matplotlib color mapping: create gorgeous drawings

Understand the matplotlib color table: create colorful drawing works

Introduction:
In the field of data visualization, matplotlib is a very powerful and widely used Python library . It offers a wealth of drawing features, but one particularly impressive feature is the ability to draw using a variety of color tables to create colorful drawings. In this article, we'll take an in-depth look at the use of matplotlib color tables and provide concrete code examples.

1. The concept of color table:
The color table is a method of mapping data values ​​to colors. It is a sequence of colors, where each color corresponds to a range of data values. Use a color table to visualize data values ​​as a continuous color gradient, making it easier to observe changes and trends in your data.

2. Color tables in matplotlib:
There are many color tables built into the matplotlib library, which can be used by calling the plt.cm module. The following are some commonly used color tables:

  1. 'viridis': This color table starts from purple and gradients through blue and green to yellow, used to represent continuous data of gradients.
  2. 'jet': This is a very common color table, starting at blue and including purple, red, and yellow, used to represent continuous data with gradients.
  3. 'cool': This color table starts with green and includes blue and cyan, used to represent cool colors.
  4. 'hot': This color table starts from black and goes through red to yellow, used to represent heat.
  5. 'rainbow': This color table starts from red, gradients from purple and cyan to green, and is used to represent continuous data of gradients.

The above are only a small part of the color tables in matplotlib. More color tables can be found in the official matplotlib documentation. Next, we'll use some concrete code examples to show how to use these colormaps.

3. Code example using matplotlib color table:
The following is a simple example showing how to use the color table in matplotlib to draw a colorful scatter plot:

import numpy as np
import matplotlib.pyplot as plt

# 生成随机数据
x = np.random.randn(1000)
y = np.random.randn(1000)
c = np.random.randn(1000)

# 绘制散点图
plt.scatter(x, y, c=c, cmap='jet')

# 添加颜色条
plt.colorbar()

# 设置标题和坐标轴标签
plt.title("Scatter Plot with Color Map")
plt.xlabel("X")
plt.ylabel("Y")

# 显示图形
plt.show()
Copy after login

In the above code, x and y are the random data we generated, and c is the data used to determine the color of each point in the scatter plot. cmap='jet'The parameter indicates that the color table 'jet' should be used. The scatter function is used to draw a scatter plot, and the colorbar function is used to add a color bar.

In addition to scatter plots, we can also use color tables to draw other types of graphics, such as curve charts, histograms, etc. The following is a sample code for drawing a curve using a color table:

import numpy as np
import matplotlib.pyplot as plt

# 生成随机数据
x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# 绘制曲线图
plt.plot(x, y1, color='c', label='sin(x)')
plt.plot(x, y2, color='m', label='cos(x)')

# 添加颜色图例
plt.legend()

# 设置标题和坐标轴标签
plt.title("Line Chart with Color Map")
plt.xlabel("X")
plt.ylabel("Y")

# 显示图形
plt.show()
Copy after login

In the above code, we use the two colors 'c' and 'm' in the color table as the color of the curve. Use the color parameter to specify the color directly instead of using a color table. The legend function is used to add a legend.

Conclusion:
By understanding the color table in matplotlib, we can use various color tables to create colorful drawings. This article introduces some commonly used color tables and provides specific code examples. I hope this article can provide you with some help in using color tables in data visualization.

The above is the detailed content of Explore matplotlib color mapping: create gorgeous drawings. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template