如何调整 Matplotlib 颜色条的位置?
颜色条是一个带有各种颜色的条,它沿着Matplotlib图表的两侧放置。它是图表中显示的颜色的图例。默认情况下,Matplotlib 颜色条的位置在右侧。可以使用Matplotlib AxesGrid Toolkit 中的函数根据我们的选择更改 Matplotlib 颜色条的位置。插入轴的放置类似于图例的放置,通过提供相对于父框的位置选项来修改位置。
Syntax: fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax)
Attribute | Description |
---|---|
cax | Axes into which the colorbar will be drawn. |
ax | Parent axes from which space for a new colorbar axes are stolen. If a list of axes is given they are resized to make room for colorbar axes. |
colorbar | Use set_label to set the label to the colorbar |
pad | relative subplot gap or fraction of original axes between colorbar and new image axes |
LogNorm | Converting number arguments or color to RGBA |
figsize | 2-tuple of floats. Figure Dimension(width, height) in inches |
add_subplot | Add an Axes to the figure as part of a subplot arrangement |
add_axes | Present in figure module of matplotlib library used to add axes to figure |
imshow | The convention used in image processing: the origin is in the top left corner. |
pcolor | Creating a pseudocolor plot with a non-regular rectangular grid. |
Matplotlib 颜色条的安装
要直接安装 matplotlib colorbar,请在 Jupyter Notebook 或 Visual Studio Code 上执行以下命令以获取结果,安装 Matplotlib-colorbar 包以便使用 colorbar 参数生成。在这里, matplotlib.pyplot用于以更简单的方式创建颜色条。
pip install matplotlib-colorbar
使用 Matplotlib 创建颜色条的另一种方法是导入 matplotlib 包,然后创建颜色条。
Python3
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
# specify dimensions of colorbar using random module
Z = np.random.rand(5, 20)
fig, ax0 = plt.subplots()
ax0.pcolor(Z)
ax0.set_title('Matplotlib-colorbar')
plt.show()
Python3
# Import packages necessary to create colorbar
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# make this example reproducible
np.random.seed(2)
#create chart
fig, ax = plt.subplots()
im = ax.imshow(np.random.rand(10,10))
ax.set_xlabel('x-axis label')
#add color bar
fig.colorbar(im)
plt.show()
Python3
#import matplotlib.pyplot to create chart
import matplotlib.pyplot as plt
import numpy as np
#create subplot
fig = plt.figure()
ax = fig.add_subplot(111)
axp = ax.imshow(np.random.randint(0, 10,( 10, 10)))
ax.set_title('Colorbar on left')
#adding colorbar and its position
cb = plt.colorbar(axp ,ax = [ax], location = 'left')
plt.show()
Python3
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# make this example reproducible
np.random.seed(1)
# create chart
fig = plt.figure()
ax = fig.add_subplot(111)
axp = ax.imshow(np.random.randint(0, 10, (10, 10)))
ax.set_title('Colorbar on left')
# Adding the colorbar
cbaxes = fig.add_axes([0.1, 0.1, 0.03, 0.8])
# position for the colorbar
cb = plt.colorbar(axp, cax = cbaxes)
plt.show()
Python3
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# make this reproducible
np.random.seed(2)
# create chart
fig, ax = plt.subplots()
im = ax.imshow(np.random.rand(10,10))
ax.set_xlabel('x-axis label')
# add color bar below chart
divider = make_axes_locatable(ax)
cax = divider.new_vertical(size='5%', pad=0.6, pack_start = True)
fig.add_axes(cax)
fig.colorbar(im, cax = cax, orientation = 'horizontal')
plt.show()
Python3
# import matplotlib packages
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
# create chart
fig, ax = plt.subplots(figsize=(4,4))
im = ax.imshow(np.random.rand(11,16))
ax.set_xlabel("x label")
# pad argument to set colorbar away from x-axis
fig.colorbar(im, orientation="horizontal", pad = 0.4)
plt.show()
Python3
# import matplotlib packages
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np; np.random.seed(1)
fig, ax = plt.subplots(figsize = (4,4))
im = ax.imshow(np.random.rand(11,16))
ax.set_xlabel("x label")
# instance is used to divide axes
divider = make_axes_locatable(ax)
cax = divider.new_vertical(size = "5%",
pad = 0.7,
pack_start = True)
fig.add_axes(cax)
# creating colorbar
fig.colorbar(im, cax = cax, orientation = "horizontal")
plt.show()
Python3
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# make this example reproducible
np.random.seed(1)
# create chart
fig, ax = plt.subplots()
im = ax.imshow(np.random.rand(15, 15))
ax.set_xlabel('x-axis label')
ax.set_title('Colorbar above chart')
# add color bar below chart
divider = make_axes_locatable(ax)
cax = divider.new_vertical(size = '5%', pad = 0.5)
fig.add_axes(cax)
fig.colorbar(im, cax = cax, orientation = 'horizontal')
plt.show()
输出:
示例 1: Matplotlib 颜色条在右侧的位置
生成一个 matplotlib 图表,其中颜色条位于图表的右侧。
蟒蛇3
# Import packages necessary to create colorbar
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# make this example reproducible
np.random.seed(2)
#create chart
fig, ax = plt.subplots()
im = ax.imshow(np.random.rand(10,10))
ax.set_xlabel('x-axis label')
#add color bar
fig.colorbar(im)
plt.show()
输出:
示例 2: Matplotlib 颜色条在左侧的位置
生成一个 Matplotlib 图表,其中颜色条位于图表的左侧。在这里,轴位置是手动设置的,颜色条使用关键字“位置”链接到现有的绘图轴。位置参数用于引用列表中多个轴的颜色条,如果您将一个轴放在列表中,则可以在此处使用该参数。
蟒蛇3
#import matplotlib.pyplot to create chart
import matplotlib.pyplot as plt
import numpy as np
#create subplot
fig = plt.figure()
ax = fig.add_subplot(111)
axp = ax.imshow(np.random.randint(0, 10,( 10, 10)))
ax.set_title('Colorbar on left')
#adding colorbar and its position
cb = plt.colorbar(axp ,ax = [ax], location = 'left')
plt.show()
输出:
这是生成颜色条并确保它在自己的轴上的简单方法。然后使用'cax' 参数指定颜色条的位置,其中为要绘制的颜色条指定轴。
蟒蛇3
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# make this example reproducible
np.random.seed(1)
# create chart
fig = plt.figure()
ax = fig.add_subplot(111)
axp = ax.imshow(np.random.randint(0, 10, (10, 10)))
ax.set_title('Colorbar on left')
# Adding the colorbar
cbaxes = fig.add_axes([0.1, 0.1, 0.03, 0.8])
# position for the colorbar
cb = plt.colorbar(axp, cax = cbaxes)
plt.show()
输出:
示例 3: Matplotlib 颜色条在图表下方的位置
要定位,图表下方的 Matplotlib 颜色栏,然后执行以下命令,
蟒蛇3
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# make this reproducible
np.random.seed(2)
# create chart
fig, ax = plt.subplots()
im = ax.imshow(np.random.rand(10,10))
ax.set_xlabel('x-axis label')
# add color bar below chart
divider = make_axes_locatable(ax)
cax = divider.new_vertical(size='5%', pad=0.6, pack_start = True)
fig.add_axes(cax)
fig.colorbar(im, cax = cax, orientation = 'horizontal')
plt.show()
输出:
Pad 参数在图表的 x 轴和颜色条之间创建填充。 pad的值越高,颜色条远离x 轴。要相对于子图移动颜色条,请使用fig.colorbar 的 pad 参数。
蟒蛇3
# import matplotlib packages
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
# create chart
fig, ax = plt.subplots(figsize=(4,4))
im = ax.imshow(np.random.rand(11,16))
ax.set_xlabel("x label")
# pad argument to set colorbar away from x-axis
fig.colorbar(im, orientation="horizontal", pad = 0.4)
plt.show()
输出:
使用make_axes_locatable的实例来划分轴并创建与图像图对齐的新轴。 Pad参数将允许在两个轴之间设置空间:
蟒蛇3
# import matplotlib packages
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np; np.random.seed(1)
fig, ax = plt.subplots(figsize = (4,4))
im = ax.imshow(np.random.rand(11,16))
ax.set_xlabel("x label")
# instance is used to divide axes
divider = make_axes_locatable(ax)
cax = divider.new_vertical(size = "5%",
pad = 0.7,
pack_start = True)
fig.add_axes(cax)
# creating colorbar
fig.colorbar(im, cax = cax, orientation = "horizontal")
plt.show()
输出:
示例 4:颜色条在图表上方的位置
要定位,图表下方的 Matplotlib 颜色栏,然后执行以下命令,
蟒蛇3
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# make this example reproducible
np.random.seed(1)
# create chart
fig, ax = plt.subplots()
im = ax.imshow(np.random.rand(15, 15))
ax.set_xlabel('x-axis label')
ax.set_title('Colorbar above chart')
# add color bar below chart
divider = make_axes_locatable(ax)
cax = divider.new_vertical(size = '5%', pad = 0.5)
fig.add_axes(cax)
fig.colorbar(im, cax = cax, orientation = 'horizontal')
plt.show()
输出: