自定义虚线样式

一条直线的破折号是通过一个破折号序列来控制的。可以使用 Line2D.set_dashes .

破折号序列是一系列以点为单位的开/关长度,例如 [3, 1] 将是由1pt空格分隔的3pt长的行。

一些功能比如 Axes.plot 支持将行属性作为关键字参数传递。在这种情况下,您可以在创建线条时设置虚线。

Note :破折号样式也可以通过 property_cycle 通过使用关键字传递破折号序列列表 破折号 骑自行车的人。此示例中未显示。

line demo dash control
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 500)
y = np.sin(x)

fig, ax = plt.subplots()

# Using set_dashes() to modify dashing of an existing line
line1, = ax.plot(x, y, label='Using set_dashes()')
line1.set_dashes([2, 2, 10, 2])  # 2pt line, 2pt break, 10pt line, 2pt break

# Using plot(..., dashes=...) to set the dashing when creating a line
line2, = ax.plot(x, y - 0.2, dashes=[6, 2], label='Using the dashes parameter')

ax.legend()
plt.show()

关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery