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()
sss Bbox(x0=211.8375, y0=256.44, x1=230.9625, y1=269.44)
[[ 211.8375  256.44  ]
 [ 230.9625  269.44  ]]
xxx Bbox(x0=212.5875, y0=5.0, x1=230.2125, y1=15.0)
Label 0, data: Bbox(x0=69.21818181818182, y0=29.0, x1=69.21818181818182, y1=29.0)
Label 1, data: 0.0Bbox(x0=61.21818181818182, y0=19.0, x1=77.21818181818182, y1=29.0)
Label 2, data: 0.2Bbox(x0=122.0909090909091, y0=19.0, x1=138.0909090909091, y1=29.0)
Label 3, data: 0.4Bbox(x0=182.9636363636364, y0=19.0, x1=198.9636363636364, y1=29.0)
Label 4, data: 0.6Bbox(x0=243.83636363636367, y0=19.0, x1=259.83636363636367, y1=29.0)
Label 5, data: 0.8Bbox(x0=304.70909090909095, y0=19.0, x1=320.70909090909095, y1=29.0)
Label 6, data: 1.0Bbox(x0=365.5818181818182, y0=19.0, x1=381.5818181818182, y1=29.0)
Label 7, data: Bbox(x0=69.21818181818182, y0=29.0, x1=69.21818181818182, y1=29.0)
Legend location: Bbox(x0=276.05, y0=127.31375, x1=383.8, y1=162.12625)
Label 0, data: first_imageBbox(x0=308.05, y0=147.22, x1=363.925, y1=158.12625)
Label 1, data: second_imageBbox(x0=308.05, y0=131.31375, x1=379.8, y1=142.22)

reference:

1. https://matplotlib.org/api/text_api.html#matplotlib.text.Annotation.get_window_extent

2. https://matplotlib.org/api/transformations.html#matplotlib.transforms.Bbox



原文地址:https://www.cnblogs.com/beatets/p/8452663.html

时间: 2024-10-12 21:28:04

Getting the pixel coordinates of text or ticks in matplotlib的相关文章

Get a specific pixel coordinates where your mouse on (cc)

#include <cv.h> #include <highgui.h> #include <stdio.h> IplImage* src=0; void on_mouse( int event, int x, int y, int flags, void* ustc) { CvFont font; cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA); if( event == CV

UnderStand Perspective Rasterization, SV_POSITION to Pixel, SV mean Systems Value

Shader "UnderStandPRR" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "Un

Image Retrieval Using Customized Bag of Features

This example shows how to create a Content Based Image Retrieval (CBIR) system using a customized bag-of-features workflow. Introduction Content Based Image Retrieval (CBIR) systems are used to find images that are visually similar to a query image.

OSG开源教程(转)

整理:荣明.王伟 北 京 2008年4月 序 第一次接触OSG是在2001年,当时开源社区刚刚兴起,还没有现在这么火.下载了OSG源码,但是在看了几个Demo之后,感觉没有什么特别之处.时隔七年之后,我再次将目光投向OSG,发现OSG确实有其独到之处,很多3D效果已经不弱于甚至超过商业软件,有感于开源力量的巨大.但是,与当前主流3D商业软件如Vega.VegaPrime.VTree.Performer等相比,开源软件的缺点也很明显,其中文档缺乏可谓其致命弱点之一.开发者只能从浩瀚的源码中,进行编

java图形图像SVG转PPM程序代写 qq:928900200

Background Many image file formats encode image data as a 2-D matrix or raster of pixel colors. The amount of available color information is fixed by the image's width and height of its resolution. If the resolution is too small, you will have proble

Supporting Multiple Screens(支持Android各种屏幕尺寸)

原文链接 本文翻译自Supporting Multiple Screens. 目前我这篇翻译应该是最全,最完整的官方文档翻译,转载我的翻译请联系本人,标明出处. Supporting Multiple Screens Android runs on a variety of devices that offer different screen sizes and densities. For applications, the Android system provides a consist

Python:2D绘图库matplotlib学习总结

本文为学习笔记----总结!大部分为demo,一部分为学习中遇到的问题总结,包括怎么设置标签为中文等.matlab博大精深,需要用的时候再继续吧. Pyplot tutorial Demo地址为:点击打开链接 一个简单的例子: # -*- coding: utf-8 -*- import matplotlib.pyplot as plt plt.plot([1, 4, 9, 16]) plt.ylabel('some numbers') plt.show() 运行结果为: 我只指定了一组list

Python_Imaging_Library中文手册、PIL中文手册、python图像处理

Python Imaging Library 中文手册 这是PIL的官方手册,2005年5月6日发布.这个版本涵盖 PIL 1.1.5的全部内容.本中文手册来自Woodpecker.org.cn 啄木鸟社区 你可以在PythonWare library找到改文档其它格式的版本以及先前的版本. 原版出处:http://www.pythonware.com/library/pil/handbook/ 目录 Python Imaging Library 中文手册 第一部分:介绍 概览 介绍 图像归档处

Video for Linux Two API Specification Revision 2.6.32【转】

转自:https://www.linuxtv.org/downloads/legacy/video4linux/API/V4L2_API/spec-single/v4l2.html Video for Linux Two API Specification Revision 2.6.32 Michael H Schimek <[email protected]> Bill Dirks Original author of the V4L2 API and documentation. Hans