如何在 matplotlib 上制作虚线填充整个域

问题描述 投票:0回答:1

enter image description here

这是相当挑剔的,我希望我能很好地描述这一点。您看到数轴上最左边的黑色虚线勾号了吗?我希望它是一条虚线(它成功了。)但我也希望它与它右侧的所有实线一样长。我注意到绘图函数拒绝执行此操作。我在其下方画了一条橙色实线,以显示虚线应填充的完整范围。由于某种原因,它绝对拒绝填充从橙色线底部到橙色线顶部的整个区域。知道为什么会这样吗?

这是我分别绘制虚线和橙色线的代码行。请注意,它们的长度不同。虚线不会占据橙色线的整个长度。

def DrawLine(xCenter, yCenter, tickHeight, center = True):
    x = xCenter * np.ones(2)
    y = linspace(yCenter, yCenter + tickHeight, num = 2)
    if (center):
        y = y - (tickHeight / 2.)
    return (x, y)
xAxis = [0., 0.03125, 0.0625, 0.09375, 0.125, 0.15625]
yAxis = [0., 0., 0., 0., 0., 0.]
height = 0.09226038271880456
for (xi, yi) in zip(xAxis, yAxis):
    (xs, ys) = DrawLine(xi, yi, height)
    ax.plot(xs, ys, color = '#ff7f0e', zorder = 0, linewidth = 1.5)
    ax.plot(xs, ys, color = 'k', zorder = 2, linewidth = 1.5, linestyle = '--')
python matplotlib
1个回答
0
投票

这是一个关于如何自定义虚线标题样式和虚线长度的小示例,因此一种配置可能适合您的需求,请参阅“自定义虚线样式”

import matplotlib.pyplot as plt

xs = [1, 2]
lw = 10
capstyles = ["butt", "round", "projecting"]

fig, ax = plt.subplots()

# Classic lines
y = 0
for capstyle in capstyles:
    y += 0.1
    ax.plot(xs, [y, y], ls="-", lw=lw, solid_capstyle=capstyle)
    ax.annotate(f"'-', {capstyle}", (2.1, y), va="center")

# Dashed lines [2, 3]
y += 0.1
for capstyle in capstyles:
    y += 0.1
    ax.plot(xs, [y, y], ls="--", lw=lw, dash_capstyle=capstyle, dashes=[2, 3])
    ax.annotate(f"'--', {capstyle}, [2, 3]", (2.1, y), va="center")

# Dashed lines [2, 2]
y += 0.1
for capstyle in capstyles:
    y += 0.1
    ax.plot(xs, [y, y], ls="--", lw=lw, dash_capstyle=capstyle, dashes=[2, 2])
    ax.annotate(f"'--', {capstyle}, [2, 2]", (2.1, y), va="center")

# Vertical lines at xs boundaries
ax.axvline(xs[0], color="black", lw=1)
ax.axvline(xs[1], color="black", lw=1)

ax.set_xlim(0, 3)
fig.show()

dashed lines plot

© www.soinside.com 2019 - 2024. All rights reserved.