📌  相关文章
📜  Matplotlib 中颜色条刻度标签的旋转

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

Matplotlib 中颜色条刻度标签的旋转

颜色条是一个轴,表示数据值到绘图中使用的颜色的映射。 matplotlib 的 pyplot 模块中的 colorbar()函数将颜色条添加到指示色标的绘图中。

典型的颜色条

有时需要旋转刻度标签以更好地可视化和理解。要更改颜色条刻度标签的旋转,请在以下位置提供所需的旋转角度:

  • cbar.ax.set_xticklabels ,如果颜色条方向是水平的
  • cbar.ax.set_yticklabels ,如果颜色条方向是垂直的

角度正值对应逆时针旋转,负值对应顺时针旋转。此外,我们可以使用“垂直”和“水平”值来代替角度的数值进行旋转。它们分别相当于 0° 和 +90°。

旋转颜色条刻度标签的步骤:

  1. 绘制图形
  2. 绘制相应的颜色条
  3. 提供刻度和刻度标签
  4. 将刻度标签的旋转设置为所需的角度

示例 1:以下程序演示了颜色条刻度标签旋转 45 度的水平颜色条。

Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
  
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
  
# Plot horizontal colorbar
cbar = plt.colorbar(
    orientation="horizontal", fraction=0.050)
  
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
          0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
  
# Rotate colorbar ticklabels by 45 degrees
# anticlockwise
cbar.ax.set_xticklabels(labels, rotation=45)
  
plt.show()


Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
  
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
  
# Plot horizontal colorbar
cbar = plt.colorbar(
    orientation="horizontal", fraction=0.050)
  
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
          0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
  
# Rotate colorbar ticklabels by 45 degrees clockwise
cbar.ax.set_xticklabels(labels, rotation=-45)
  
plt.show()


Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
  
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
  
# Plot vertical colorbar
cbar = plt.colorbar(
    orientation="vertical", fraction=0.050)
  
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
          0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
  
# Rotate colorbar ticklabels by 30 degrees
# anticlockwise
cbar.ax.set_yticklabels(labels, rotation=30)
  
plt.show()


Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
  
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
  
# Plot vertical colorbar
cbar = plt.colorbar(
    orientation="vertical", fraction=0.050)
  
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
          0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
  
# Rotate colorbar ticklabels by 30 degrees clockwise
cbar.ax.set_yticklabels(labels, rotation=-30)
  
plt.show()


Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
  
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
  
# Plot horizontal colorbar
cbar = plt.colorbar(
    orientation="horizontal", fraction=0.050)
  
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
          0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
  
# Rotate colorbar ticklabels by 90 degrees
# anticlockwise using "vertical" value
cbar.ax.set_xticklabels(labels,
                        rotation="vertical")
  
plt.show()


Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
  
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
  
# Plot vertical colorbar
cbar = plt.colorbar(
    orientation="vertical", fraction=0.050)
  
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
          0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
  
# Rotate colorbar ticklabels by 270 degrees
# anticlockwise
cbar.ax.set_yticklabels(labels, rotation=270)
plt.show()


输出:

示例 2:以下程序演示了颜色条刻度标签旋转 -45 度的水平颜色条。

蟒蛇3

# Import libraries
import matplotlib.pyplot as plt
import numpy as np
  
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
  
# Plot horizontal colorbar
cbar = plt.colorbar(
    orientation="horizontal", fraction=0.050)
  
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
          0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
  
# Rotate colorbar ticklabels by 45 degrees clockwise
cbar.ax.set_xticklabels(labels, rotation=-45)
  
plt.show()

输出:

示例 3:以下程序演示了颜色条刻度标签旋转 30 度的垂直颜色条。

蟒蛇3

# Import libraries
import matplotlib.pyplot as plt
import numpy as np
  
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
  
# Plot vertical colorbar
cbar = plt.colorbar(
    orientation="vertical", fraction=0.050)
  
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
          0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
  
# Rotate colorbar ticklabels by 30 degrees
# anticlockwise
cbar.ax.set_yticklabels(labels, rotation=30)
  
plt.show()

输出:

示例 4:以下程序演示了颜色条刻度标签旋转 -30 度的垂直颜色条。

蟒蛇3

# Import libraries
import matplotlib.pyplot as plt
import numpy as np
  
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
  
# Plot vertical colorbar
cbar = plt.colorbar(
    orientation="vertical", fraction=0.050)
  
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
          0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
  
# Rotate colorbar ticklabels by 30 degrees clockwise
cbar.ax.set_yticklabels(labels, rotation=-30)
  
plt.show()

输出:

示例 5:以下程序演示了带有颜色条刻度标签垂直旋转的水平颜色条。

蟒蛇3

# Import libraries
import matplotlib.pyplot as plt
import numpy as np
  
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
  
# Plot horizontal colorbar
cbar = plt.colorbar(
    orientation="horizontal", fraction=0.050)
  
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
          0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
  
# Rotate colorbar ticklabels by 90 degrees
# anticlockwise using "vertical" value
cbar.ax.set_xticklabels(labels,
                        rotation="vertical")
  
plt.show()

输出:

示例 6:以下程序演示了颜色条刻度标签旋转 270 度的垂直颜色条。

蟒蛇3

# Import libraries
import matplotlib.pyplot as plt
import numpy as np
  
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
  
# Plot vertical colorbar
cbar = plt.colorbar(
    orientation="vertical", fraction=0.050)
  
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
          0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
  
# Rotate colorbar ticklabels by 270 degrees
# anticlockwise
cbar.ax.set_yticklabels(labels, rotation=270)
plt.show()

输出: