其他分享
首页 > 其他分享> > matplotlib图形中添加文本

matplotlib图形中添加文本

作者:互联网

matplotlib图形中添加文本

plt.text(x, y, s, fontsize= 15,\  

verticalalignment="top",

         horizontalalignment="right"

        family = "fantasy", color = "r", style = "italic", weight = "light",\  

        bbox = dict(facecolor = "r", alpha = 0.2))  

 

参数说明

x,y:位置(position)

s:该position需要展示的值

family:字体

bbox:给字体添加框, facecolor 设置框体的颜色, alpha: 透明度

color:字体颜色

fontsize:字体大小[size in points | ‘xx-small’ | ‘x-small’ | ‘small’ | ‘medium’ | ‘large’ | ‘x-large’ | ‘xx-large’ ]

weight :字体粗细[0-1000数值 | ‘ultralight’ | ‘light’ | ‘normal’ | ‘regular’ | ‘book’ | ‘medium’ | ‘roman’ | ‘semibold’ | ‘demibold’ | ‘demi’ | ‘bold’ | ‘heavy’ | ‘extra bold’ | ‘black’ ]

verticalalignment:垂直对齐方式 ,参数:[ ‘center’ | ‘top’ | ‘bottom’ | ‘baseline’ ]

horizontalalignment:水平对齐方式 ,参数:[ ‘center’ | ‘right’ | ‘left’ ]

 

范例

import numpy as np

import matplotlib.pyplot as plt

 

# 设置画布颜色为 blue

fig, ax = plt.subplots(facecolor='blue')

 

# y 轴数据

data = [[5, 25, 50, 20],

        [4, 23, 51, 17],

        [6, 22, 52, 19]]

X = np.arange(4)

 

plt.bar(X + 0.00, data[0], color='darkorange', width=0.25, label='A')

plt.bar(X + 0.25, data[1], color='steelblue', width=0.25, label="B")

plt.bar(X + 0.50, data[2], color='violet', width=0.25, label='C')

 

ax.set_title("Figure 2")

plt.legend()

 

# 添加文字描述

W = [0.00, 0.25, 0.50]  # 偏移量

for i in range(3):

    for a, b in zip(X + W[i], data[i]):  # zip拆包

        plt.text(a, b, "%.0f" % b, ha="center", va="bottom")  # 格式化字符串,保留0位小数

 

plt.xlabel("Group")

plt.ylabel("Num")

plt.text(0.0, 48, "TEXT")  # 在(0,48)这个位置,显示TEXT这个值

plt.show()

 

 

 

标签:plt,facecolor,color,0.25,matplotlib,添加,text,文本,data
来源: https://blog.csdn.net/Kwoky/article/details/116348314