📜  Python中的 matplotlib.pyplot.clabel()

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

Python中的 matplotlib.pyplot.clabel()

等高线图或水平图是一种在二维平面上显示三维曲面的方法。它将一个输出变量 z 和 y 轴上的两个预测变量 x 和 y 绘制为等高线。通常,此类轮廓也称为 z 切片。
mathplotlib.pyplot 中的 clabel() 方法用于将标签添加到类实例中的线条轮廓以支持轮廓绘图。

下面是一些程序来说明 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')

输出: