How you can Upload Textual content to Matplotlib Plots (With Examples)


You’ll be able to simply upload textual content to a Matplotlib plot by way of the use of the matplotlib.pyplot.textual content() serve as, which makes use of refer to syntax:

matplotlib.pyplot.textual content(x, y, s, fontdict=None)

the place:

  • x: The x-coordinate of the textual content
  • y: The y-coordinate of the textual content
  • s: The wool of textual content
  • fontdict: A dictionary to override the default textual content homesĀ 

This instructional displays a number of examples of tips on how to significance this serve as in apply.

Instance 1: Upload a Unmarried Textual content to a Matplotlib Plot

Please see code displays tips on how to assemble a scatterplot and upload a unmarried piece of textual content to the plot:

import matplotlib.pyplot as plt

#assemble information
x = [3, 6, 8, 12, 14]
y = [4, 9, 14, 12, 9]

#assemble scatterplot
plt.leak(x, y)

#upload textual content at (x, y) coordinates = (6, 9.5)
plt.textual content(6, 9.5, 'Right here we proceed')

Matplotlib add text to plot

Instance 2: Upload A couple of Texts to a Matplotlib Plot

Please see code displays tips on how to assemble a scatterplot and upload a couple of items of textual content to the plot:

import matplotlib.pyplot as plt

#assemble information
x = [3, 6, 8, 12, 14]
y = [4, 9, 14, 12, 9]

#assemble scatterplot
plt.leak(x, y)

#upload textual content at (x, y) coordinates = (6, 9.5)
plt.textual content(6, 9.5, 'A work of textual content')

#upload any other piece of textual content
plt.textual content(8, 13, 'Some other piece of textual content')

Add multiple pieces of text to Matplotlib plot

Instance 3: Alter the Textual content Houses

To change the textual content homes, we will assemble a dictionary that specifies the font homes.

Please see code displays how to take action:

import matplotlib.pyplot as plt

#assemble information
x = [3, 6, 8, 12, 14]
y = [4, 9, 14, 12, 9]

#assemble scatterplot
plt.leak(x, y)

font = {'population': 'serif',
        'colour':  'crimson',
        'weight': 'daring',
        'measurement': 20
        }

#upload textual content with customized font
plt.textual content(6, 9.5, 'A work of textual content', fontdict=font)

Customize text in Matplotlib

Instance 4: Upload a Field Across the Textual content

Please see code displays tips on how to upload a field across the textual content:

import matplotlib.pyplot as plt

#assemble information
x = [3, 6, 8, 12, 14]
y = [4, 9, 14, 12, 9]

#assemble scatterplot
plt.leak(x, y)

font = {'population': 'serif',
        'colour':  'crimson',
        'weight': 'daring',
        'measurement': 20
        }

field = {'facecolor': 'none',
       'edgecolor': 'inexperienced',
       'boxstyle': 'spherical'
      }

#upload textual content with customized font
plt.textual content(6, 9.5, 'A work of textual content', fontdict=font, bbox=field)

Text with box around it in Matplotlib

Extra Assets

How you can Annotate Matplotlib Scatterplots
How you can Exchange Font Sizes on a Matplotlib Plot

Leave a Comment