How to use OffsetImage inside AnnotationbBox In Python

 

Annotation of points with text is often used while plotting graphs. However, we can also annotate the points using images.  Any emoji image can be used as an annotation using the OffsetImage and AnnotationbBox. In the example given below, we have plotted the cosine values on a graph. An emoji is used as a marker for each point.

 

Approach:

  1. Like the previous example, we import all the necessary libraries.
  2. The np.linspace() returns evenly spaced numbers over a specified interval. It takes parameters: start value, end value, and the total number of values we want. Here we get 20 values between 0 and 10.
  3. The np.cos() returns the cosine of all the x_values.
  4. Using plt.imread(), the emoji image is read from a file into an array. It takes up the filename as a parameter.
  5. Through matplotlib.offsetbox(), we set our emoji image as the offsetImage. The parameters taken include the image and the zoom value. Setting the zoom value to a constant ensures that the size of the emoji remains the same even when the graph is magnified.
  6. The np.atleast_1d() converts inputs provided to arrays with at least one dimension. Passing x and y values returns arrays consisting of all of their values.
  7. The zip() function iterates through x and y values. We go through each value of x and its corresponding y values and create an annotation box.  AnnotationBbox() function takes the offset box (image_box that we created) and the (x, y) value where the emoji is plotted. The frameon value is kept False to remove a square box surrounding the emoji.
  8. The Add_artist() method takes up the annotation box we created (ab) and then returns this added artist.
  9. Finally, we plot the graph using ax.plot()  and set the colour to green.
  10. The plt.show() displays the graph on the screen.

 

Example:

 

Python3




# importing all important libraries
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
import numpy as np
 
# plotting the graph
fig, ax = plt.subplots()
 
x = np.linspace(0, 10, 20)
y = np.cos(x)
ax.set_title('Cosine Values', fontsize=15)
ax.set_xlabel('Value')
ax.set_ylabel('Cosine')
 
# reading the image
image = plt.imread('emoji.png')
 
# OffsetBox
image_box = OffsetImage(image, zoom=0.1)
 
# creating annotation for each point
# on the graph
x, y = np.atleast_1d(x, y)
 
# for each value of (x,y), we create
# an annotation
for x0, y0 in zip(x, y):
    ab = AnnotationBbox(image_box, (x0, y0), frameon=False)
    ax.add_artist(ab)
 
ax.plot(x,y, c='green')
plt.show()


Output:



Emojis as markers in Matplotlib

Prerequisite:  Matplotlib

When we plot graphs, quite often there’s a need to highlight certain points and show them explicitly. This makes our demonstration more precise and informative.  It can be achieved with the use of markers. Markers in Matplotlib are a way to emphasize each point that is plotted. They are nothing but ‘plotted points’ which can be used with a graph plot, scatter plot, etc. in different colors, shapes, and sizes. Instead of using the traditional markers, we will see how we can use emojis as markers in our graph.

We will discuss the following two ways used to add emojis as markers:

  1. Using Unicode Characters
  2. Using OffsetImage inside AnnotationbBox

Similar Reads

Using Unicode Characters

Unicode is a standard for the consistent encoding and handling of text, numbers, etc. We can also represent emojis through Unicode characters. For example, a grinning face can be represented through U+1F600. Unicode can be implemented by different character encodings like UTF-8, UTF-16, and UTF-32. In this example, we use the UTF-8 encoding system....

Using OffsetImage inside AnnotationbBox

...