加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python 绘图与可视化 matplotlib text 与transform

发布时间:2020-12-20 12:55:08 所属栏目:Python 来源:网络整理
导读: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-tex

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

简单使用:(更多例子见应用)

#参数介绍:
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,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,transform=axs.transAxes)#这里的,transform=axs.transAxes就是轴坐标,大概意思就是左边距离横坐标轴长的0.1倍,下面距离纵坐标轴的0.90倍,如果不写的话默认就是data坐标,即0.1代表横轴的0.1个单位,即坐标点

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读