【matplotlib可视化】文字图例

本文详细介绍了matplotlib库在Python中进行文本图例的使用,包括Figure和Axes上的文本操作,如text、title、xlabel、ylabel、annotate等,以及Tick上的文本设置和图例legend的创建与定制。通过实例解析了各种方法的参数和应用,是掌握matplotlib图形注释和图例的实用教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文字图例

Figure和Axes上的文本

Matplotlib具有广泛的文本支持,包括对数学表达式的支持、对栅格和矢量输出的TrueType支持、具有任意旋转的换行分隔文本以及Unicode支持。

下面的命令是介绍了通过pyplot API和objected-oriented API分别创建文本的方式。

pyplot API OO API description
text text Axes的任意位置添加text。
title set_title Axes添加title
figtext text Figure的任意位置添加text.
suptitle suptitle Figure添加title
xlabel set_xlabel Axes的x-axis添加label
ylabel set_ylabel Axes的y-axis添加label
annotate annotate Axes的任意位置添加带有可选箭头的标注.

1. text

  • pyplot API:matplotlib.pyplot.text(x, y, s, fontdict=None, **kwargs)
  • OO API:Axes.text(self, x, y, s, fontdict=None, **kwargs)
  • 参数:此方法接受以下描述的参数:
  • s:此参数是要添加的文本。
  • xy:此参数是放置文本的点(x,y)。
  • fontdict:此参数是一个可选参数,并且是一个覆盖默认文本属性的字典。如果fontdict为None,则由rcParams确定默认值。

返回值:此方法返回作为创建的文本实例的文本。

fontdict主要参数具体介绍,更多参数请参考官网说明

Property Description
alpha float or None 该参数指透明度,越接近0越透明,越接近1越不透明
backgroundcolor color 该参数指文本的背景颜色,具体matplotlib支持颜色如下
bbox dict with properties for patches.FancyBboxPatch 这个是用来设置text周围的box外框
color or c color 指的是字体的颜色
fontfamily or family {FONTNAME, ‘serif’, ‘sans-serif’, ‘cursive’, ‘fantasy’, ‘monospace’} 该参数指的是字体的类型
fontproperties or font or font_properties font_manager.FontProperties or str or pathlib.Path
fontsize or size float or {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’} 该参数指字体大小
fontstretch or stretch {a numeric value in range 0-1000, ‘ultra-condensed’, ‘extra-condensed’, ‘condensed’, ‘semi-condensed’, ‘normal’, ‘semi-expanded’, ‘expanded’, ‘extra-expanded’, ‘ultra-expanded’} 该参数是指从字体中选择正常、压缩或扩展的字体
fontstyle or style {‘normal’, ‘italic’, ‘oblique’} 该参数是指字体的样式是否倾斜等
fontweight or weight {a numeric value in range 0-1000, ‘ultralight’, ‘light’, ‘normal’, ‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’, ‘demibold’, ‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’}
horizontalalignment or ha {‘center’, ‘right’, ‘left’} 该参数是指选择文本左对齐右对齐还是居中对齐
label object
linespacing float (multiple of font size)
position (float, float)
rotation float or {‘vertical’, ‘horizontal’} 该参数是指text逆时针旋转的角度,“horizontal”等于0,“vertical”等于90。我们可以根据自己设定来选择合适角度
verticalalignment or va {‘center’, ‘top’, ‘bottom’, ‘baseline’, ‘center_baseline’}
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import numpy as np

#fontdict学习的案例
#学习的过程中请尝试更换不同的fontdict字典的内容,以便于更好的掌握
#---------设置字体样式,分别是字体,颜色,宽度,大小
font1 = {
   
   'family': 'SimSun',#华文楷体
         'alpha':0.7,#透明度
        'color':  'purple',
        'weight': 'normal',
        'size': 16,
        }
font2 = {
   
   'family': 'Times New Roman',
        'color':  'red',
        'weight': 'normal',
        'size': 16,
        }
font3 = {
   
   'family': 'serif',
        'color':  'blue',
        'weight': 'bold',
        'size': 14,
        }
font4 = {
   
   'family': 'Calibri',
        'color':  'navy',
        'weight': 'normal',
        'size': 17,
        }
#-----------四种不同字体显示风格-----
 
#-------建立函数----------
x = np.linspace(0.0, 5.0, 100)
y = np.cos(2*np.pi*x) * np.exp(-x/3)
#-------绘制图像,添加标注----------
plt.plot(x, y, '--')
plt.title('震荡曲线', fontdict=font1)
#------添加文本在指定的坐标处------------
plt.text(2, 0.65, r'$\cos(2 \pi x) \exp(-x/3)$', fontdict=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值