matplotlib 中,可以使用 linestyle 参数来设置线条样式,其中包括虚线样式。具体操作如下:

  1. 导入 matplotlib 库:

    import matplotlib.pyplot as plt
    
  2. 创建一个 Figure 对象和一个 Axes 对象:

    fig, ax = plt.subplots()
    
  3. plot 方法中设置 linestyle 参数为虚线样式:

    ax.plot(x, y, linestyle='--')
    

    其中,xy 分别是数据的横纵坐标。

  4. 可以进一步设置虚线的样式,例如设置虚线的长度和间隔:

    ax.plot(x, y, linestyle=(0, (5, 10)))
    

    其中,(0, (5, 10)) 表示虚线的样式为长度为 5、间隔为 10 的线段。

完整代码示例:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

fig, ax = plt.subplots()
ax.plot(x, y, linestyle='--')

plt.show()

输出结果如下:

![虚线图](https://cdn.jsdelivr.net/gh/Yousazoe/picture/img/20211201195037.jpg

origin中画图怎么增加虚线

原文地址: https://www.cveoy.top/t/topic/hxhF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录