📜  在 matplotlib 中设置颜色条范围

📅  最后修改于: 2022-05-13 01:55:18.987000             🧑  作者: Mango

在 matplotlib 中设置颜色条范围

你好极客!在本文中,我们将尝试使用 matplotlib Python模块设置颜色范围。 Matplotlib 允许我们进行大范围的 Colorbar 自定义。 Colorbar 只是 plt.Axes 的一个实例。它根据图表中的数据提供数量与颜色比例的比例。设置范围将颜色限制为一个子部分,颜色条错误地传达了数据下限与其上限相当的信息。通过两个不同的限制,您可以控制颜色栏的范围和图例。

要求: Matplotlib、NumPy

用于安装 Matplotlib

pip install matplotlib

用于安装 Numpy。

pip install numpy

让我们通过逐步实施来理解:

第1步:

导入所需的库并设置一些通用数据。

Python3
import numpy as np
import matplotlib.pyplot as plt
  
# setup some generic data
N = 37
x, y = np.mgrid[:N, :N]
Z = (np.cos(x*0.2) + np.sin(y*0.3))


Python3
Zpos = np.ma.masked_less(Z, 0)
Zneg = np.ma.masked_greater(Z, 0)


Python3
# plot just the positive data and save the
# color "mappable" object returned by ax1.imshow
pos = ax1.imshow(Zpos, cmap = 'Blues', interpolation = 'none')


Python3
fig, (ax1, ax2, ax3) = plt.subplots(figsize=(13, 3), 
                                    ncols = 3)
  
pos_neg_clipped = ax3.imshow(Z, 
                             cmap = 'RdBu', 
                             vmin = -1.2,
                             vmax = 1.2)
# Add minorticks on the colorbar to make 
# it easy to read the values off the colorbar.
color_bar = fig.colorbar(pos_neg_clipped, 
                         ax = ax3,
                         extend = 'both')
  
color_bar.minorticks_on()
plt.show()


Python3
import numpy as np
import matplotlib.pyplot as plt
  
# setup some generic data
N = 37
x, y = np.mgrid[:N, :N]
Z = (np.cos(x*0.2) + np.sin(y*0.3))
  
# mask out the negative and positive values
Zpositive = np.ma.masked_less(Z, 0)
Znegative = np.ma.masked_greater(Z, 0)
  
fig, (ax1, ax2, ax3) = plt.subplots(figsize = (13, 3),
                                    ncols = 3)
  
# plot just the positive data and save the
# color "mappable" object returned by ax1.imshow
pos = ax1.imshow(Zpositive, cmap = 'Blues')
  
# add the colorbar using the figure's method,
fig.colorbar(pos, ax = ax1)
  
# repeat everything above for the negative data
neg = ax2.imshow(Znegative, cmap = 'Reds_r')
fig.colorbar(neg, ax = ax2)
  
# Plot both positive and negative values between +/- 1.2
pos_neg_clipped = ax3.imshow(Z, cmap = 'RdBu',
                             vmin = -1.2, 
                             vmax = 1.2)
  
# Add minorticks on the colorbar to make 
# it easy to read the values off the colorbar.
color_bar = fig.colorbar(pos_neg_clipped, 
                         ax = ax3,
                         extend = 'both')
  
color_bar.minorticks_on()
plt.show()


第2步:

掩盖负值和正值。

蟒蛇3

Zpos = np.ma.masked_less(Z, 0)
Zneg = np.ma.masked_greater(Z, 0)

 

第 3 步:

将数据显示为图像,即在 2D 规则栅格上。

蟒蛇3

# plot just the positive data and save the
# color "mappable" object returned by ax1.imshow
pos = ax1.imshow(Zpos, cmap = 'Blues', interpolation = 'none')

第四步:

绘制 +/- 1.2 之间的正值和负值

蟒蛇3

fig, (ax1, ax2, ax3) = plt.subplots(figsize=(13, 3), 
                                    ncols = 3)
  
pos_neg_clipped = ax3.imshow(Z, 
                             cmap = 'RdBu', 
                             vmin = -1.2,
                             vmax = 1.2)
# Add minorticks on the colorbar to make 
# it easy to read the values off the colorbar.
color_bar = fig.colorbar(pos_neg_clipped, 
                         ax = ax3,
                         extend = 'both')
  
color_bar.minorticks_on()
plt.show()

输出:

下面是完整的实现:

蟒蛇3

import numpy as np
import matplotlib.pyplot as plt
  
# setup some generic data
N = 37
x, y = np.mgrid[:N, :N]
Z = (np.cos(x*0.2) + np.sin(y*0.3))
  
# mask out the negative and positive values
Zpositive = np.ma.masked_less(Z, 0)
Znegative = np.ma.masked_greater(Z, 0)
  
fig, (ax1, ax2, ax3) = plt.subplots(figsize = (13, 3),
                                    ncols = 3)
  
# plot just the positive data and save the
# color "mappable" object returned by ax1.imshow
pos = ax1.imshow(Zpositive, cmap = 'Blues')
  
# add the colorbar using the figure's method,
fig.colorbar(pos, ax = ax1)
  
# repeat everything above for the negative data
neg = ax2.imshow(Znegative, cmap = 'Reds_r')
fig.colorbar(neg, ax = ax2)
  
# Plot both positive and negative values between +/- 1.2
pos_neg_clipped = ax3.imshow(Z, cmap = 'RdBu',
                             vmin = -1.2, 
                             vmax = 1.2)
  
# Add minorticks on the colorbar to make 
# it easy to read the values off the colorbar.
color_bar = fig.colorbar(pos_neg_clipped, 
                         ax = ax3,
                         extend = 'both')
  
color_bar.minorticks_on()
plt.show()

输出: