Adding an arbitrary line to a matplotlib plot in ipython notebook

后端 未结 5 1355
孤独总比滥情好
孤独总比滥情好 2020-12-07 14:41

I\'m rather new to both python/matplotlib and using it through the ipython notebook. I\'m trying to add some annotation lines to an existing graph and I can\'t figure out ho

5条回答
  •  半阙折子戏
    2020-12-07 15:05

    Matplolib now allows for 'annotation lines' as the OP was seeking. The annotate() function allows several forms of connecting paths and a headless and tailess arrow, i.e., a simple line, is one of them.

    ax.annotate("",
                xy=(0.2, 0.2), xycoords='data',
                xytext=(0.8, 0.8), textcoords='data',
                arrowprops=dict(arrowstyle="-",
                          connectionstyle="arc3, rad=0"),
                )
    

    In the documentation it says you can draw only an arrow with an empty string as the first argument.

    From the OP's example:

    %matplotlib notebook
    import numpy as np
    import matplotlib.pyplot as plt
    
    np.random.seed(5)
    x = np.arange(1, 101)
    y = 20 + 3 * x + np.random.normal(0, 60, 100)
    plt.plot(x, y, "o")
    
    
    # draw vertical line from (70,100) to (70, 250)
    plt.annotate("",
                  xy=(70, 100), xycoords='data',
                  xytext=(70, 250), textcoords='data',
                  arrowprops=dict(arrowstyle="-",
                                  connectionstyle="arc3,rad=0."), 
                  )
    
    # draw diagonal line from (70, 90) to (90, 200)
    plt.annotate("",
                  xy=(70, 90), xycoords='data',
                  xytext=(90, 200), textcoords='data',
                  arrowprops=dict(arrowstyle="-",
                                  connectionstyle="arc3,rad=0."), 
                  )
    
    plt.show()
    

    Example inline image

    Just as in the approach in gcalmettes's answer, you can choose the color, line width, line style, etc..

    Here is an alteration to a portion of the code that would make one of the two example lines red, wider, and not 100% opaque.

    # draw vertical line from (70,100) to (70, 250)
    plt.annotate("",
                  xy=(70, 100), xycoords='data',
                  xytext=(70, 250), textcoords='data',
                  arrowprops=dict(arrowstyle="-",
                                  edgecolor = "red",
                                  linewidth=5,
                                  alpha=0.65,
                                  connectionstyle="arc3,rad=0."), 
                  )
    

    You can also add curve to the connecting line by adjusting the connectionstyle.

提交回复
热议问题