Python中的 matplotlib.pyplot.clabel()
等高线图或水平图是一种在二维平面上显示三维曲面的方法。它将一个输出变量 z 和 y 轴上的两个预测变量 x 和 y 绘制为等高线。通常,此类轮廓也称为 z 切片。
mathplotlib.pyplot 中的 clabel() 方法用于将标签添加到类实例中的线条轮廓以支持轮廓绘图。
Syntax: matplotlib.pyplot.clabel(CS, levels=None, **kwargs)
Parameters:
- CS: The ContourSet to label.
- levels: A list of level values, that should be labeled. The list must be a subset of CS.levels. If not given,all levels are labeled. It is an optional argument(default value is None).
- fontsize: Size in points or relative size e.g., ‘smaller’, ‘x-large’. See Text.set_size for accepted string values.
- colors: The label colors-
- If None, the color of each label matches the color of the corresponding contour.
- If one string color, e.g., colors = ‘r’ or colors = ‘red’, all labels will be plotted in this color.
- If a tuple of matplotlib color args (string, float, rgb, etc), different labels will be plotted in different colors in the order specified.
下面是一些程序来说明 matplotlib.pyplot.clabel() 的使用:
示例 1:使用默认颜色创建带有标签的简单等高线图。 clabel 的 inline 参数将控制标签是否绘制在轮廓的线段上,删除标签下方的线。
Python3
# importing the required libraries
import numpy
import matplotlib.pyplot
# creating the graph
delta = 0.025
x = numpy.arange(-3.0, 3.0, delta)
y = numpy.arange(-2.0, 2.0, delta)
X, Y = numpy.meshgrid(x, y)
Z1 = numpy.exp(-X**2 - Y**2)
Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
# adding labels to the line contours
fig, ax = matplotlib.pyplot.subplots()
CS = ax.contour(X, Y, Z)
ax.clabel(CS, inline=1, fontsize=10)
ax.set_title('Simplest default with labels')
Python3
# importing the required libraries
import numpy
import matplotlib.pyplot
# creating the graph
delta = 0.025
x = numpy.arange(-3.0, 3.0, delta)
y = numpy.arange(-2.0, 2.0, delta)
X, Y = numpy.meshgrid(x, y)
Z1 = numpy.exp(-X**2 - Y**2)
Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
# adding labels to the line contours
fig, ax = matplotlib.pyplot.subplots()
CS = ax.contour(X, Y, Z)
manual_locations = [(-1, -1.4), (-0.62, -0.7),
(-2, 0.5), (1.7, 1.2),
(2.0, 1.4), (2.4, 1.7)]
ax.clabel(CS, inline=1, fontsize=10, manual=manual_locations)
ax.set_title('labels at selected locations')
Python3
# importing the required libraries
import numpy
import matplotlib.pyplot
# creating the graph
delta = 0.025
x = numpy.arange(-3.0, 3.0, delta)
y = numpy.arange(-2.0, 2.0, delta)
X, Y = numpy.meshgrid(x, y)
Z1 = numpy.exp(-X**2 - Y**2)
Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
# adding labels to the line contours
fig, ax = matplotlib.pyplot.subplots()
CS = ax.contour(X, Y, Z, 6,
colors='k',
)
ax.clabel(CS, fontsize=9, inline=1)
ax.set_title('Single color - negative contours dashed')
Python3
# importing the required libraries
import numpy
import matplotlib.pyplot
# creating the graph
delta = 0.025
x = numpy.arange(-3.0, 3.0, delta)
y = numpy.arange(-2.0, 2.0, delta)
X, Y = numpy.meshgrid(x, y)
Z1 = numpy.exp(-X**2 - Y**2)
Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
# adding labels to the line contours
matplotlib.rcParams['contour.negative_linestyle'] = 'solid'
fig, ax = matplotlib.pyplot.subplots()
CS = ax.contour(X, Y, Z, 6,
colors='k',
)
ax.clabel(CS, fontsize=9, inline=1)
ax.set_title('Single color - negative contours solid')
Python3
# importing the required libraries
import numpy
import matplotlib.pyplot
# creating the graph
delta = 0.025
x = numpy.arange(-3.0, 3.0, delta)
y = numpy.arange(-2.0, 2.0, delta)
X, Y = numpy.meshgrid(x, y)
Z1 = numpy.exp(-X**2 - Y**2)
Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
# adding labels to the line contours
fig, ax = matplotlib.pyplot.subplots()
CS = ax.contour(X, Y, Z, 6,
linewidths=np.arange(.5, 4, .5),
colors=('r', 'green', 'blue',
(1, 1, 0), '#afeeee', '0.5')
)
ax.clabel(CS, fontsize=9, inline=1)
ax.set_title('Crazy lines')
输出:
示例 2:可以通过提供位置列表(在数据坐标中)手动放置轮廓标签。有关交互式放置,请参见 ginput_manual_clabel.py。
Python3
# importing the required libraries
import numpy
import matplotlib.pyplot
# creating the graph
delta = 0.025
x = numpy.arange(-3.0, 3.0, delta)
y = numpy.arange(-2.0, 2.0, delta)
X, Y = numpy.meshgrid(x, y)
Z1 = numpy.exp(-X**2 - Y**2)
Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
# adding labels to the line contours
fig, ax = matplotlib.pyplot.subplots()
CS = ax.contour(X, Y, Z)
manual_locations = [(-1, -1.4), (-0.62, -0.7),
(-2, 0.5), (1.7, 1.2),
(2.0, 1.4), (2.4, 1.7)]
ax.clabel(CS, inline=1, fontsize=10, manual=manual_locations)
ax.set_title('labels at selected locations')
输出:
示例 3:您可以强制所有轮廓为相同颜色。
Python3
# importing the required libraries
import numpy
import matplotlib.pyplot
# creating the graph
delta = 0.025
x = numpy.arange(-3.0, 3.0, delta)
y = numpy.arange(-2.0, 2.0, delta)
X, Y = numpy.meshgrid(x, y)
Z1 = numpy.exp(-X**2 - Y**2)
Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
# adding labels to the line contours
fig, ax = matplotlib.pyplot.subplots()
CS = ax.contour(X, Y, Z, 6,
colors='k',
)
ax.clabel(CS, fontsize=9, inline=1)
ax.set_title('Single color - negative contours dashed')
输出:
示例 4:您可以将负轮廓设置为实线而不是虚线:
Python3
# importing the required libraries
import numpy
import matplotlib.pyplot
# creating the graph
delta = 0.025
x = numpy.arange(-3.0, 3.0, delta)
y = numpy.arange(-2.0, 2.0, delta)
X, Y = numpy.meshgrid(x, y)
Z1 = numpy.exp(-X**2 - Y**2)
Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
# adding labels to the line contours
matplotlib.rcParams['contour.negative_linestyle'] = 'solid'
fig, ax = matplotlib.pyplot.subplots()
CS = ax.contour(X, Y, Z, 6,
colors='k',
)
ax.clabel(CS, fontsize=9, inline=1)
ax.set_title('Single color - negative contours solid')
输出:
示例 5:您可以手动指定轮廓的颜色。
Python3
# importing the required libraries
import numpy
import matplotlib.pyplot
# creating the graph
delta = 0.025
x = numpy.arange(-3.0, 3.0, delta)
y = numpy.arange(-2.0, 2.0, delta)
X, Y = numpy.meshgrid(x, y)
Z1 = numpy.exp(-X**2 - Y**2)
Z2 = numpy.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
# adding labels to the line contours
fig, ax = matplotlib.pyplot.subplots()
CS = ax.contour(X, Y, Z, 6,
linewidths=np.arange(.5, 4, .5),
colors=('r', 'green', 'blue',
(1, 1, 0), '#afeeee', '0.5')
)
ax.clabel(CS, fontsize=9, inline=1)
ax.set_title('Crazy lines')
输出: