📜  Python中的 Matplotlib.pyplot.colorbar()函数

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

Python中的 Matplotlib.pyplot.colorbar()函数

颜色条是从标量值到颜色的映射的可视化。在 Matplotlib 中,它们被绘制到一个专用轴中。

注意:颜色条通常通过Figure.colorbar或其 pyplot 包装器pyplot.colorbar 创建,它在内部使用 make_axes 和 Colorbar。作为最终用户,您很可能不必显式调用此模块中的方法或实例化类。

Python中的matplotlib.pyplot.colorbar()

matplotlib 的 pyplot 模块中的colorbar()函数将颜色条添加到指示色标的绘图中。

下面的例子说明了 matplotlib.pyplot 中的 matplotlib.pyplot.colorbar()函数:

示例 #1: 向散点图添加水平颜色条。

Python3
# Python Program illustrating
# pyplot.colorbar() method
import numpy as np
import matplotlib.pyplot as plt
  
# Dataset
# List of total number of items purchased 
# from each products
purchaseCount = [100, 200, 150, 23, 30, 50,
                 156, 32, 67, 89]
  
# List of total likes of 10 products
likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]
  
# List of Like/Dislike ratio of 10 products
ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56, 
         1.28, 1.09, 1.02]
  
# scatterplot
plt.scatter(x=purchaseCount, y=likes, c=ratio, cmap="summer")
  
plt.colorbar(label="Like/Dislike Ratio", orientation="horizontal")
plt.show()


Python3
# Python Program illustrating
# pyplot.colorbar() method
import matplotlib.pyplot as plt
  
# creates four Axes
fig, axes = plt.subplots(nrows=2, ncols=2)
  
for ax in axes.flat:
    im = ax.imshow(np.random.random((10, 10)), vmin=0, vmax=1)
  
plt.colorbar(im, ax=axes.ravel().tolist())
  
plt.show()


Python3
# Python Program illustrating
# pyplot.colorbar() method
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
  
x = np.linspace(0, 5, 100)
N = 7
  
# colormap
cmap = plt.get_cmap('jet', N)
  
fig, ax1 = plt.subplots(1, 1, figsize=(8, 6))
  
for i, n in enumerate(np.linspace(0, 2, N)):
    y = x*i+n
    ax1.plot(x, y, c=cmap(i))
  
plt.xlabel('x-axis')
plt.ylabel('y-axis')
  
# Normalizer
norm = mpl.colors.Normalize(vmin=0, vmax=2)
  
# creating ScalarMappable
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
  
plt.colorbar(sm, ticks=np.linspace(0, 2, N))
  
  
plt.show()


输出:

示例 #2:将单个颜色条添加到多个子图。

蟒蛇3

# Python Program illustrating
# pyplot.colorbar() method
import matplotlib.pyplot as plt
  
# creates four Axes
fig, axes = plt.subplots(nrows=2, ncols=2)
  
for ax in axes.flat:
    im = ax.imshow(np.random.random((10, 10)), vmin=0, vmax=1)
  
plt.colorbar(im, ax=axes.ravel().tolist())
  
plt.show()

输出:

示例 #3: 将颜色条添加到不可映射的对象。

蟒蛇3

# Python Program illustrating
# pyplot.colorbar() method
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
  
x = np.linspace(0, 5, 100)
N = 7
  
# colormap
cmap = plt.get_cmap('jet', N)
  
fig, ax1 = plt.subplots(1, 1, figsize=(8, 6))
  
for i, n in enumerate(np.linspace(0, 2, N)):
    y = x*i+n
    ax1.plot(x, y, c=cmap(i))
  
plt.xlabel('x-axis')
plt.ylabel('y-axis')
  
# Normalizer
norm = mpl.colors.Normalize(vmin=0, vmax=2)
  
# creating ScalarMappable
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
  
plt.colorbar(sm, ticks=np.linspace(0, 2, N))
  
  
plt.show()

输出: