Getting the pixel coordinates of text or ticks in matplotlib

The exact pixel coordinates of title, labels, legends or ticks are important information for the training data of deep learning. 

 

import numpy as np import matplotlib.pyplot as plt plt.plot([1,2],label="first_image") plt.plot([2,1],label="second_image") leg = plt.legend() tit = plt.title("sss") xla = plt.xlabel("xxx") plt.gcf().canvas.draw() ticks = [t for t in plt.gca().get_xticklabels()] print(tit.get_text(),str(tit.get_window_extent())) # return the context and pixel coordinates of 'tit' print(np.array(tit.get_window_extent())) print(xla.get_text(),str(xla.get_window_extent())) for i, t in enumerate(ticks): print ("Label "+str(i)+", data: "+t.get_text()+str(t.get_window_extent())) print ("Legend location: "+str(leg.get_window_extent())) for i, l in enumerate(leg.texts): print ("Label "+str(i)+", data: "+l.get_text()+str(l.get_window_extent())) pass #plt.savefig("test.png") plt.show()

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgzjs.html