How to use Unicode Characters In Python

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. 

Approach:

In the example given below, we plot the squared values for the given list of numbers on graphs.  The x_values is a list of numbers from 1 to 5 and y_values is another list consisting of squared values of each number in the list x_values.

  1. The matplotlib.pyplot.subplots() function in the pyplot module of the matplotlib library is used to create a figure and a set of subplots. Although it takes a number of parameters, for simplicity we only specify the figure size. It returns a tuple (fig, ax) giving a single figure fig with an array of axes ax.
  2. The ax.plot() takes a list of x and y values, marker, marker size (ms), and color (c). Note that there’s a dollar($) sign before and after the Unicode characters while specifying the marker.  One can easily find Unicode characters for the emojis they wish to.
  3. Finally, using ax.set(), we set the title, x, and y labels.
  4. The plt.show() displays the graph on the screen.

Example: 

Python3




# importing matplotlib module
import matplotlib.pyplot as plt
 
# defining a list of x and v values
x_values = [1, 2, 3, 4, 5]
y_values = [x*x for x in x_values]
 
# plotting the graph
fig, ax = plt.subplots(figsize=(10, 6))
 
# check that the marker value is given as
# '$U0001F601$'
ax.plot(x_values, y_values, marker='$\U0001F601$', ms=20, c='green')
ax.set_title('Squared Values', fontsize=15)
ax.set_xlabel('Value')
ax.set_ylabel('Square of Value')
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

...