三行代码  ›  专栏  ›  技术社区  ›  keithIHS

如何消除线段两端的填充

  •  1
  • keithIHS  · 技术社区  · 3 周前

    我正在创建一种叠加在现有matplotlib图上的水平条形图。我想要一个酒吧的传奇。

    下面是一个例子

    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    y_data = (0,0,1,0)
    ax.step((0., 0.1, 0.2, 0.6, 0.8, 1.), (0., 0., 0.5, 1., 0.5, 0.), where='pre')
    ax.plot((0.1,0.6),(0.1,0.1), color = 'red', linewidth = 10, label = "line 1")
    ax.plot((0.2,0.8),(0.2,0.2), color = 'green', linewidth = 10, label = "line 2")
    ax.set_ylim(0,1); ax.set_xlim(0,1)
    ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
    plt.tight_layout()
    
    

    线段的起点稍早于线段端点的坐标,终点稍晚于线段端点坐标。该数量似乎取决于线条的厚度。如何消除开头和结尾的多余部分? 我想过画矩形,但后来我该怎么画这个传说呢?

    2 回复  |  直到 3 周前
        1
  •  1
  •   BigBen    3 周前

    使用 Axes.hlines :

    ax.hlines(0.1, 0.1, 0.6, color='red', lw=10, label='line1')
    ax.hlines(0.2, 0.2, 0.8, color='green', lw=10, label='line2')
    

    输出:

    enter image description here

        2
  •  0
  •   keithIHS    3 周前

    如有疑问,请阅读文档: ax.axspan()

    推荐文章