python text & comments & math expression settings | math expression settings in python plotting

本篇文章将介绍如何在Matplotlib中设置文本、注释和数学表达式,以便更好地呈现数据,提高可视化效果。



1. Text settings in Matplotlib

1.1 Plain Text Settings

提示:这里可以添加本文要记录的大概内容:

Text can be added with the text() function. The syntax of the text() function is as follows:

text(x, y, s, fontdict=None, withdash=False, **kwargs)
  1. x and y represent the horizontal and vertical coordinates of the text,
  2. s represents the text content to be added,
  3. fontdict represents a dictionary of font attributes,
  4. Withdash indicates whether to use a dotted line to frame the text,
  5. kwargs is an optional parameter.
    Text can be added in Matplotlib by following code
import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [4, 5, 6]

plt.plot(x, y)

plt.text(2, 5, "This is a text", fontsize=12, color="red")

plt.show()

insert image description here
First, use the plot() function to draw a straight line, and then use the text() function to add a piece of text at the coordinate point (2,5), the font size is 12, and the color is red.

1.2 Text settings with arrows

In addition to the text() function, we can also use the annotate() function to add annotations. Its syntax format is as follows:

annotate(s, xy, xytext=None, xycoords='data', 
	textcoords='offset points', arrowprops=None, **kwargs)
  1. s represents the text content to be added,
  2. xy represents the coordinate point pointed by the arrow,
  3. xytext indicates the coordinate point of the text, the default is None, which means use the same coordinate point as xy,
  4. xycoords and textcoords represent the coordinate system of xy and xytext respectively, the default is "data", which represents the data coordinate system,
  5. arrowprops indicates the properties of the arrow, and kwargs is an optional parameter.
import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [4, 5, 6]

plt.plot(x, y)

plt.annotate("This is an annotation", xy=(2, 5), xytext=(2.5, 5.5), 
             arrowprops=dict(facecolor='red', shrink=0.05), fontsize=12)

plt.show()

insert image description here


2. Mathematical expression setting in Matplotlib

Mathematical expressions can also be used to add text or annotations to present content such as mathematical formulas. The mathematical expression syntax supported by Matplotlib is similar to LaTeX, which allows us to easily add various common mathematical symbols and functions.
We can use mathematical expressions in text in two modes: inline mode and inline mode. Inline mode uses a single dollar sign $ to surround mathematical expressions, for example:

import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y)
plt.text(2, 5, r"$y = \sqrt{x}$", fontsize=12)
plt.show()

insert image description here


Mathematical expressions are used in the text, where \sqrt{x} is used to represent the operation of finding the root sign.
In contrast, inline mode uses a pair of $$ symbols to surround mathematical expressions, for example:

import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [4, 5, 6]

plt.plot(x, y)

plt.annotate(r"$\sum_{i=1}^n i = \frac{n(n+1)}{2}$", 
xy=(2, 5), xytext=(2.5, 5.5), 
arrowprops=dict(facecolor='red', shrink=0.05), fontsize=12)
plt.show()

insert image description here

3. Font settings in Matplotlib

In Matplotlib, we can also set the font properties of text or comments through the fontdict parameter, including font size, color, font type, etc. The fontdict parameter is a dictionary containing key-value pairs for various font properties.

import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [4, 5, 6]

plt.plot(x, y)

font = {
    
    'family': 'Times New Roman',
         'color':  'darkred',
         'weight': 'normal',
         'size': 14,
         }

plt.text(2, 5, "This is a text", fontdict=font)

plt.show()

Guess you like

Origin blog.csdn.net/m0_58857684/article/details/131036793