#193 Annotate matplotlib chart

  •  

     

     

     

     

    You can easily spot an interesting point on your chart adding an arrow and some text using the annotate function of matplotlib. See more concerning its use here.

     

     

     

     

     

     

    
    # Library
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    # Basic chart
    df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
    plt.plot( 'x', 'y', data=df, linestyle='none', marker='o')
    
    # Annotate with text + Arrow
    plt.annotate(
    # Label and coordinate
    'This point is interesting!', xy=(25, 50), xytext=(0, 80),
    
    # Custom arrow
    arrowprops=dict(facecolor='black', shrink=0.05)
    )
    
    
  •  

     

     

     

     

     

    To add a rectangle, you need to use the patches utility of matplotlib.

     

     

     

     

     

     

    
    # libraries
    import matplotlib.patches as patches
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    # Data
    df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
    
    # Plot
    fig1 = plt.figure()
    ax1 = fig1.add_subplot(111)
    ax1.plot( 'x', 'y', data=df, linestyle='none', marker='o')
    
    # Add rectangle
    ax1.add_patch(
    patches.Rectangle(
    (20, 25), # (x,y)
    50, # width
    50, # height
    # You can add rotation as well with 'angle'
    alpha=0.3, facecolor="red", edgecolor="black", linewidth=3, linestyle='solid'
    )
    )
    
    
  •  

     

     

     

     

    A circle can be added through the patches utility as well.

     

     

     

     

     

     

     

    
    # Libraries
    import matplotlib.patches as patches
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    # Data
    df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
    
    # Plot
    fig1 = plt.figure()
    ax1 = fig1.add_subplot(111)
    ax1.plot( 'x', 'y', data=df, linestyle='none', marker='o')
    
    # Annotation
    ax1.add_patch(
    patches.Circle(
    (40, 35),           # (x,y)
    30,                    # radius
    alpha=0.3, facecolor="green", edgecolor="black", linewidth=1, linestyle='solid'
    )
    )
    
    
  •  

     

     

     

     

    To add a segment, the easiest way is to use the plot function

     

     

     

     

     

     

    
    # Library
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    # Basic chart
    df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
    plt.plot( 'x', 'y', data=df, linestyle='none', marker='o')
    
    # Annotation
    plt.plot([80, 40], [30, 90], color="skyblue", lw=5, linestyle='solid', label="_not in legend")
    
    
  •  

     

     

     

     

    You can add horizontal and vertical lines to your plots with axhline and axvline. These lines go from one extremity of your plot to the other.

     

     

     

     

     

     

    
    # Library
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    # Plot
    df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
    plt.plot( 'x', 'y', data=df, linestyle='none', marker='o')
    
    # Annotation
    plt.axvline(40, color='r')
    plt.axhline(40, color='green')
    
    
  •  

     

     

     

     

    You can write math equation on your chart through the text function.

     

     

     

     

     

     

    
    # Library
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    # plot
    df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
    plt.plot( 'x', 'y', data=df, linestyle='none', marker='o')
    
    # Annotation
    plt.text(40, 00, r'equation: $\sum_{i=0}^\infty x_i$', fontsize=20)
    
    
  •  

     

     

     

     

    You can add an ellipse using patches

     

     

     

     

     

     

    
    # libraries
    import matplotlib.patches as patches
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    # Data
    df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
    
    # Plot
    fig1 = plt.figure()
    ax1 = fig1.add_subplot(111)
    ax1.plot( 'x', 'y', data=df, linestyle='none', marker='o')
    ax1.add_patch(
    patches.Ellipse(
    (40, 35), # (x,y)
    30, # width
    100, # height
    45, # radius
    alpha=0.3, facecolor="green", edgecolor="black", linewidth=1, linestyle='solid'
    )
    )
    
    

Leave a Reply

Your email address will not be published.