注意
跳转到底部 下载完整的示例代码。
线型#
简单的线型可以使用字符串“solid”(实线)、“dotted”(点线)、“dashed”(虚线)或“dashdot”(点划线)来定义。通过提供一个虚线元组 (offset, (on_off_seq))
可以实现更精细的控制。例如,(0, (3, 10, 1, 15))
表示 (3点线段,10点空白,1点线段,15点空白),无偏移;而 (5, (10, 3))
则表示 (10点线段,3点空白),但跳过前5点线段。另请参阅 Line2D.set_linestyle
。“dotted”(点线)、“dashed”(虚线)和“dashdot”(点划线)样式的具体开关序列是可配置的
rcParams["lines.dotted_pattern"]
(默认值:[1.0, 1.65]
)rcParams["lines.dashed_pattern"]
(默认值:[3.7, 1.6]
)rcParams["lines.dashdot_pattern"]
(默认值:[6.4, 1.6, 1.0, 1.6]
)
注意:虚线样式也可以通过 Line2D.set_dashes
进行配置,如 虚线样式配置 所示,并且可以使用关键词 dashes 将虚线序列列表传递给 property_cycle 中的循环器(cycler)。
import matplotlib.pyplot as plt
import numpy as np
linestyle_str = [
('solid', 'solid'), # Same as (0, ()) or '-'
('dotted', 'dotted'), # Same as ':'
('dashed', 'dashed'), # Same as '--'
('dashdot', 'dashdot')] # Same as '-.'
linestyle_tuple = [
('loosely dotted', (0, (1, 10))),
('dotted', (0, (1, 5))),
('densely dotted', (0, (1, 1))),
('long dash with offset', (5, (10, 3))),
('loosely dashed', (0, (5, 10))),
('dashed', (0, (5, 5))),
('densely dashed', (0, (5, 1))),
('loosely dashdotted', (0, (3, 10, 1, 10))),
('dashdotted', (0, (3, 5, 1, 5))),
('densely dashdotted', (0, (3, 1, 1, 1))),
('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))),
('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]
def plot_linestyles(ax, linestyles, title):
X, Y = np.linspace(0, 100, 10), np.zeros(10)
yticklabels = []
for i, (name, linestyle) in enumerate(linestyles):
ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black')
yticklabels.append(name)
ax.set_title(title)
ax.set(ylim=(-0.5, len(linestyles)-0.5),
yticks=np.arange(len(linestyles)),
yticklabels=yticklabels)
ax.tick_params(left=False, bottom=False, labelbottom=False)
ax.spines[:].set_visible(False)
# For each line style, add a text annotation with a small offset from
# the reference point (0 in Axes coords, y tick value in Data coords).
for i, (name, linestyle) in enumerate(linestyles):
ax.annotate(repr(linestyle),
xy=(0.0, i), xycoords=ax.get_yaxis_transform(),
xytext=(-6, -12), textcoords='offset points',
color="blue", fontsize=8, ha="right", family="monospace")
fig, (ax0, ax1) = plt.subplots(2, 1, figsize=(7, 8), height_ratios=[1, 3],
layout='constrained')
plot_linestyles(ax0, linestyle_str[::-1], title='Named linestyles')
plot_linestyles(ax1, linestyle_tuple[::-1], title='Parametrized linestyles')
plt.show()

脚本总运行时间: (0 分钟 1.864 秒)