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個單位,即坐標點