Java教程

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()

 

 

 

这篇关于matplotlib图形中添加文本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!