How to add text in a plot in python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, We are going to see how to add text inside the plot in Matplotlib. The matplotlib.pyplot.text[] function is used to add text inside the plot. The syntax adds text at an arbitrary location of the axes. It also supports mathematical expressions.

    Syntax: matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]

    Parameters:

    • where x, y – coordinates
    • s – text to be added inside the plot[string]
    • fontdict – optional parameter. It overrides the default text properties
    • **kwargs – text properties

    Example 1: Adding mathematical equations inside the plot.

    Python3

    import matplotlib.pyplot as plt

    import numpy as np

    x = np.arange[-10, 10, 0.01]

    y = x**2

    plt.text[-5, 60, 'Parabola $Y = x^2$', fontsize = 22]

    plt.plot[x, y, c='g']

    plt.xlabel["X-axis", fontsize = 15]

    plt.ylabel["Y-axis",fontsize = 15]

    plt.show[]

    Output:

    Example 2: Adding rectangular box around the text by using the keyword ‘bbox’. bbox is a dictionary of Rectangle properties.

    Python3

    import matplotlib.pyplot as plt

    import numpy as np

    x = np.arange[-10, 10, 0.01]

    y = x**2

    plt.xlabel["X-axis", fontsize = 15]

    plt.ylabel["Y-axis",fontsize = 15]

    plt.text[-5, 60, 'Parabola $Y = x^2$', fontsize = 22

             bbox = dict[facecolor = 'red', alpha = 0.5]]

    plt.plot[x, y, c = 'g']

    plt.show[]

    Output:

    Example 3: Adding the text “Sine wave” inside the plot.

    Python3

    import matplotlib.pyplot as plt

    import numpy as np

    x = np.arange[0, 10, 0.1]

    y = np.sin[x]

    plt.plot[x,y]

    plt.text[3.5, 0.9, 'Sine wave', fontsize = 23]

    plt.xlabel['X-axis', fontsize = 15]

    plt.ylabel['Y-axis', fontsize = 15]

    plt.show[]

    Output: 

    Example 4: Using annotation along with text inside plot

    Python3

    import matplotlib.pyplot as plt

    import numpy as np

    x = ['Rani', 'Meena', 'Raju', 'Jhansi', 'Ram']

    y = [5, 7, 9, 2, 6]

    plt.bar[x,y]

    plt.text[3, 7, 'Student Marks'

             fontsize = 18, color = 'g']

    plt.xlabel['Students', fontsize = 15]

    plt.ylabel['Marks', fontsize = 15]

    plt.annotate['Highest scored', xy = [2.4, 8], 

                 fontsize = 16, xytext = [3, 9], 

                 arrowprops = dict[facecolor = 'red'],

                 color = 'g']

    plt.show[]

    Output:


    How do I insert text in matplotlib?

    Basic text commands.
    text[] - add text at an arbitrary location to the Axes; matplotlib. ... .
    xlabel[] - add an axis label to the x-axis; matplotlib. ... .
    ylabel[] - add an axis label to the y-axis; matplotlib. ... .
    title[] - add a title to the Axes; matplotlib. ... .
    figtext[] - add text at an arbitrary location to the Figure; matplotlib..

    How do you add a caption to a plot in Python?

    Create x and y data points using numpy..
    Create a new figure or activate an existing figure using figure[] method..
    Plot the scatter points with x and y data points..
    To add caption to the figure, use text[] method..
    Adjust the padding between and around the subplots..
    To display the figure, use show[] method..

    How do I show text in matplotlib?

    Show activity on this post..
    text. You can use text to place a text in the figure. ... .
    annotate. You can use annotate to produce a text somewhere in the figure. ... .
    AnchoredText. You can use an AnchoredText from offsetbox: from matplotlib.offsetbox import AnchoredText a = AnchoredText["d={}"..

    How do you show a value in a plot in Python?

    Just call the plot[] function and provide your x and y values. Calling the show[] function outputs the plot visually.

    Chủ Đề