Python 绘图与可视化 matplotlib text 与transform
2024-09-08 12:22:55
Text
为plots添加文本或者公式,反正就是添加文本了
参考链接:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.text.html#matplotlib.pyplot.text
参考链接(应用):https://matplotlib.org/tutorials/text/text_intro.html#sphx-glr-tutorials-text-text-intro-py
补充:
获取设置的text:
参考链接:https://matplotlib.org/3.1.1/api/text_api.html
get_text:
print( frames_names['text%s'%algorithm_list[i].__name__].get_text)#注意没加括号也能
get_text()
print( frames_names['text%s'%algorithm_list[i].__name__].get_text())
简单使用:(更多例子见应用)
#参数介绍:
matplotlib.pyplot.text(x, y, s, fontdict=None, withdash=<deprecated parameter>, **kwargs)
s:添加的文本
time_text = ax.text(0.1, 0.9, '', transform=ax.transAxes)
#替换文本时可以
time_text.set_text(time_template %(0.1*i))
其中time_template = 'time = %.1fs',这样是为了替换时方便一些,若是只添加一次的话,直接在上面写全就好
出现的问题:
*)下面代码不显示添加的text,最后查看原因发现是axs.cla()的原因,所以在animate里直接添加axs.text(....)
time_template='time=%.2fs'
time_text=axs.text(0.1,0.90,"",transform=axs.transAxes)
# def init():
# time_text.set_text("")
# return time_text
frames=bidirectional_bubble_sort(original_data_object)
def animate(fi):
bars=[]
if len(frames)>fi:
axs.cla()
# axs.text(0.1,0.90,time_template%(0.1*fi),transform=axs.transAxes)#所以这样
time_text.set_text(time_template%(0.1*fi))#这个必须没有axs.cla()才行 axs.set_title('bubble_sort_visualization')
axs.set_xticks([])
axs.set_yticks([])
bars=axs.bar(list(range(Data.data_count)),#个数
[d.value for d in frames[fi]],#数据
1, #宽度
color=[d.color for d in frames[fi]]#颜色
).get_children()
return bars
anim=animation.FuncAnimation(fig,animate,frames=len(frames), interval=frame_interval,repeat=False)
transform
transform就是转换的意思,是不同坐标系的转换
参考链接:https://matplotlib.org/users/transforms_tutorial.html
拿上面的例子来讲,在为plots添加text时,就用到了坐标转换
xs.text(0.1,0.90,time_template%(0.1*fi),transform=axs.transAxes)#这里的,transform=axs.transAxes就是轴坐标,大概意思就是左边距离横坐标轴长的0.1倍,下面距离纵坐标轴的0.90倍,如果不写的话默认就是data坐标
,即0.1代表横轴的0.1个单位,即坐标点
最新文章
- LeetCode 258. Add Digits
- 在css嵌套中的html的table里的字左右不对齐
- day11---异步IO\数据库\队列\缓存
- jQuery水平下拉菜单实现
- 第一个React程序HelloWorld
- Linux系统性能监控
- wampsever 数据库初体验
- .net错误处理机制(转)
- Shell下通过echo+telnet在远端执行命令
- Blend制作TextButton和ImageButton
- Linux驱动编写(块设备驱动代码)
- 外部VBS的调用
- java操作redis redis连接池
- 集合-TreeSet-Comparable
- 黑马程序员Java基础班+就业班课程笔记全发布(持续更新)
- Audio Source组件及相关API
- WEB学习笔记11-高可读性的HTML之如何设置网页标题层级
- App Technical Support
- 微信小程序ios上时间字符串转换为时间戳时会报错,在开发工具上和安卓手机上运行成功
- java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 不能使用 &#39;(未知的)&#39;;文件已在使用中
热门文章
- vm|vmware workstation 15|14 pro 激活|密钥|序列号|许可证
- 【java集合总结】-- 数组总结+自己封装数组类
- TensorFlow GPU版本的安装与调试
- Invitation Cards POJ - 1511
- Android开发环境搭建(个人环境非通用)
- Java8——Optional
- c语言命名规范图
- zz深度学习在美团配送 ETA 预估中的探索与实践
- js对url进行编码的方法(encodeURI和 encodeURICompoent())
- c++的CreateFile导致内存不能为written错误