Matplotlib:如何制作由点(圆圈)组成的虚线?

gla*_*anz 4 python matplotlib

我有两个平滑的依赖 y1(x) 和 y2(x),其中 x 的分布不规则。我希望用虚线 ( linestyle = ':')描述这些依赖关系。我现在得到的* .pdf文件显示在这里

这是代码:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x  = [0, 1, 2,  3,  5,  7, 13, 14]
y1 = [3, 5, 6,  8,  7,  6,  9, 10]
y2 = [1, 7, 8, 10, 14, 18, 20, 23]

ax.plot(x, y1, 
        linestyle = ':',
        linewidth = 4,
        color = 'Green')

ax.plot(x, y2, 
        linestyle = ':',
        linewidth = 4,
        color = 'Blue')

ax.set_ylabel('y(x)')
ax.set_xlabel('x')

plt.savefig("./test_dotted_line.pdf")
Run Code Online (Sandbox Code Playgroud)

我玩过dashes = [2,2](和其他组合) and dash_capstyle = 'round',但结果看起来很糟糕。

是否有可能有一条由“圆”点组成的虚线?

ZHA*_*uji 5

试试这个线条样式:

ax.plot(x, y1, 
    linestyle = (0,(0.1,2)),
    dash_capstyle = 'round',
    linewidth = 4,
    color = 'Green')
Run Code Online (Sandbox Code Playgroud)

输出是这样的: 输出


Fra*_*nes 0

消除linewidth。然后它打印小方块——足够好了吗?

您还可以使用 来对正方形进行四舍五入dash_capstyle = "round"