matplotlib图形中添加文本
2021/5/2 10:29:04
本文主要是介绍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图形中添加文本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-04TiDB 资源管控的对撞测试以及最佳实践架构
- 2024-07-03万字长文聊聊Web3的组成架构
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现