Text object in matplotlib doesnt respond to zooming properly

2024/12/9 21:53:42

. Hello, everyone.

I recently try to add text object in my plot. But when I zoom into the text, the text size remains unchanged. What I want to have is that the text size will increase when I zoom in and will decrease when I zoom out.

import matplotlib as mpl
fig=plt.figure()
ax1=fig.add_subplot(111)
ax1.text('','', '',position=[0.5,0.5], text='Y', fontsize='xx-small' )

Any help is appreciated. Thanks~

Supplement-UTC+8 30/04/2013 9:40 AM

Thanks for the suggestion from tcaswell. TextPath does achieve part of my purposes.

I found there is no documentation about textpath in official matplotlib website, so I view the source code to see what it does. Finally, I achieved a not excellent but satisfactory result as follows.

from matplotlib.textpath import TextPath
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.path import Pathfig=plt.figure()
ax1=fig.add_subplot(111)
tp1=TextPath((0.5,0.5), r'How do you turn this on?', size=1)
polygon=tp1.to_polygons()
for a in polygon:p1=patches.Polygon(a)ax1.add_patch(p1)

The not excellent part of this code is that it doesn't support rotation and export the text as filled polygon. Is there any easy way to rotate the text? Can I export the text as non-filled polygon?

Answer

When you create a Polygon instance, you can specify many keyword arguments, including setting fill = False (see details here):

from matplotlib.textpath import TextPath
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.path import Pathfig=plt.figure()
ax1=fig.add_subplot(111)
ax1.set_ylim(-1 , 3)
ax1.set_xlim(-3, 15)
tp1=TextPath((0.0,0.5), r'How do you turn this on?', size=1)
polygon=tp1.to_polygons()
for a in polygon:p1=patches.Polygon(a, fill=False)ax1.add_patch(p1)plt.show()

figure image

https://en.xdnf.cn/q/73346.html

Related Q&A

Python unit testing code which calls OS/Module level python functions

I have a python module/script which does a few of theseAt various nested levels inside the script I take command line inputs, validate them, apply sensible defaults I also check if a few directories ex…

How do I connect/disconnect/configure a wireless network in python?

Im looking to see if there is a way to connect or disconnect to a wireless network in python, preferably a way that would work for both public and secured networks if I supplied the password. If I can …

CSRF protection on AJAX authentication in Flask

Id like to AJAXify both a login and a signup form on a site. Up to now Ive been using WTForms mainly for its built-in CSRF protetion, but for this project I didnt feel like it was worth it -- an extra …

Pandas groupby and Multiindex

Is there any opportunity in pandas to groupby data by MultiIndex? By this i mean passing to groupby function not only keys but keys and values to predefine dataframe columns?a = np.array([foo, foo,…

How to dump YAML with explicit references?

Recursive references work great in ruamel.yaml or pyyaml: $ ruamel.yaml.dump(ruamel.yaml.load(&A [ *A ])) &id001 - *id001However it (obviously) does not work on normal references: $ ruamel.yaml…

How to set a Pydantic field value depending on other fields

from pydantic import BaseModelclass Grafana(BaseModel):user: strpassword: strhost: strport: strapi_key: str | None = NoneGRAFANA_URL = f"http://{user}:{password}@{host}:{port}"API_DATASOURCES…

Cascade multiple RNN models for N-dimensional output

Im having some difficulty with chaining together two models in an unusual way. I am trying to replicate the following flowchart:For clarity, at each timestep of Model[0] I am attempting to generate an …

Pandas Flatten a list of list within a column?

I am trying to flatten a column which is a list of lists:var var2 0 9122532.0 [[458182615.0], [79834910.0]] 1 79834910.0 [[458182615.0], [9122532.0]] 2 458182615.0 [[79834910.0], [9122…

How to use libxml2 with python on macOs?

Im on OSX Lion and I have libxml2 installed (by default) and I have python installed (by default) but they dont talk to one another. Whats the simplest way to make this work on Lion?$ python -c "…

SMTP Authentication error while while sending mail from outlook using python language

import smtplibsmtpObj = smtplib.SMTP(smtp.office365.com, 587)smtpObj.ehlo()smtpObj.starttls()smtpObj.login([email protected], abcde)smtpObj.sendmail([email protected], [email protected], Subject: So l…