matplotlib的基本用法(五)——添加annotation

本文介绍了使用Matplotlib进行数据可视化的基础操作,包括如何绘制线性图表、设置坐标轴样式、添加注解等,并提供了完整的代码示例。

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

文章作者:Tyan
博客:noahsnail.com  |  优快云  |  简书

本文主要是关于matplotlib的一些基本用法。

  • Demo
import matplotlib.pyplot as plt
import numpy as np

# 绘制普通图像
x = np.linspace(-3, 3, 50)
y = 2 * x + 1

plt.figure()
plt.plot(x, y)

# 将上、右边框去掉
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# 设置x轴的位置及数据在坐标轴上的位置
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
# 设置y轴的位置及数据在坐标轴上的位置
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

# 定义(x0, y0)点
x0 = 1
y0 = 2 * x0 + 1

# 绘制(x0, y0)点
plt.scatter(x0, y0, s = 50, color = 'blue')
# 绘制虚线
plt.plot([x0, x0], [y0, 0], 'k--', lw = 2.5)
# 绘制注解一
plt.annotate(r'$2 * x + 1 = %s$' % y0, xy = (x0, y0), xycoords = 'data', xytext = (+30, -30), \
             textcoords = 'offset points', fontsize = 16, arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3, rad = .2'))

# 绘制注解二
plt.text(-3, 3, r'$Test\ text. \mu \sigma_i, \alpha_i$', fontdict = {'size': 16, 'color': 'red'})
plt.show()
  • 结果

设置结果图

参考资料

  1. https://www.youtube.com/user/MorvanZhou
import matplotlib.pyplot as plt import numpy as np # -------------------------- # 数据准备 # -------------------------- categories = ['住宿业', '文创产业', '其他服务业'] percentages = [58.4, 8.8, 32.8] # 业态占比 colors_inner = ['#1f77b4', '#2ca02c', '#7f7f7f'] # 内环颜色(蓝、绿、灰) # 外环指标数据 annotations = [ "🏠 15间以下小微主体 83%\n📉 淡旺季价差3.6倍", "🖼️ 同质化率72%\n🎨 非遗工坊仅13%", "📲 数字化覆盖率58%\n🤖 技术应用率<5%" ] # 经营效率指数(右侧条形图) efficiency = [62, 41, 53] # -------------------------- # 画布设置 # -------------------------- fig, ax = plt.subplots(figsize=(12, 8)) plt.rcParams['font.sans-serif'] = ['SimHei'] # 解决中文显示问题 plt.rcParams['axes.unicode_minus'] = False # -------------------------- # 绘制内环(业态占比) # -------------------------- wedges_inner, texts = ax.pie( percentages, labels=categories, colors=colors_inner, startangle=90, wedgeprops=dict(width=0.3, edgecolor='w'), textprops=dict(color='w', weight='bold', fontsize=12) ) # 中心文字 plt.text(0, 0, 'N=433', ha='center', va='center', fontsize=20, color='#444444') # -------------------------- # 绘制外环(指标注释) # -------------------------- # 计算每个区块的角度 angles = np.deg2rad([90, 90 - 58.4/100 * 360, 90 - (58.4+8.8)/100 * 360]) for i, (angle, annotation) in enumerate(zip(angles, annotations)): # 计算文本位置 offset = 0.55 # 外环半径 x = offset * np.cos(angle) y = offset * np.sin(angle) # 添加警示符号 if i == 0: ax.annotate('❗', (x*1.1, y*1.1), color='red', fontsize=14, ha='center') elif i == 1: ax.annotate('❗', (x*0.95, y*0.95), color='red', fontsize=14, ha='center') # 添加指标文本 ax.annotate( annotation, (x, y), xytext=(1.3*x, 1.3*y), textcoords='data', ha='center', va='center', fontsize=10, color='#444444', arrowprops=dict(arrowstyle='-', color='gray', alpha=0.5) ) # -------------------------- # 右侧条形图(经营效率) # -------------------------- ax_eff = fig.add_axes([0.72, 0.15, 0.2, 0.3]) # 设置位置和大小 bars = ax_eff.barh(
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值