SoFunction
Updated on 2024-11-21

Introduction to Matplotlib and simple use of pyplot - text annotation and arrows

When using pyplot to draw graphs, sometimes you need to label some text on the graph, and if the curves are close together, it is better to use arrows to point out the correspondence between the labeled text and the curves. Here we introduce the use of text labeling and arrows.

Add annotations for use by pyplot or subplot. The following parameters can be selected.

text(tx,ty,fontsize=fs,verticalalignment=va,horizontalalignment=ha,...)

Among them, tx and ty specify the location of the text, va and ha specify the way it can be top, bottom, center or left, right, center, you can also make the text with a border, the shape of the border can also be an arrow, and specify the direction.

Add arrows to use, called in a similar way to text. The following parameters are optional.

annotate(text,xy=(tx0,ty0),xytext=(tx1,ty1),arrowprops=dict(arrowstyle="->",connectionstyle="arc3"))

Where text is the text with the arrow, xy is the location of the arrow, the end point, xytext is the starting point, arrowtypes specify the style of the arrow, more content or see the manual it.

The effect is as follows.

The code is as follows, with just a few modifications from the previous subplot, the

#!/usr/bin/env python

import numpy as np
import  as plt

def f1(t):
 return (-t)*(2**t)

def f2(t):
 return (2**t)*(3**t)

t = (0.0,5.0,0.02)

(figsize=(8,7),dpi=98)
p1 = (211)
p2 = (212)

label_f1 = "$f(t)=e^{-t} \cos (2 \pi t)$"
label_f2 = "$g(t)=\sin (2 \pi t) \cos (3 \pi t)$"

(t,f1(t),"g-",label=label_f1)
(t,f2(t),"r-.",label=label_f2,linewidth=2)

([0.0,5.01,-1.0,1.5])

p1.set_ylabel("v",fontsize=14)
p1.set_title("A simple example",fontsize=18)
(True)
#()

tx = 2
ty = 0.9
(tx,ty,label_f1,fontsize=15,verticalalignment="top",horizontalalignment="right")

([0.0,5.01,-1.0,1.5])
p2.set_ylabel("v",fontsize=14)
p2.set_xlabel("t",fontsize=14)
#()
tx = 2
ty = 0.9
(tx,ty,label_f2,fontsize=15,verticalalignment="bottom",horizontalalignment="left")

('',xy=(1.8,0.5),xytext=(tx,ty),arrowprops=dict(arrowstyle="->",connectionstyle="arc3"))

()

Don't make something too complicated when it's already simple.

summarize

Above is the whole content of this article on the introduction to Matplotlib and the simple use of pyplot - text annotation and arrows, I hope it will be helpful to you. Interested friends can continue to refer to this site:

Drawing contour plots with matplotlib in detail

Python draw animation through matplotlib simple example

Introduction to matplotlib, installation and simple example code

If there are deficiencies, welcome to leave a message to point out. Thank you friends for the support of this site!