Matplotlib Plaats tekst in het plot

In deze tutorial leren we hoe u tekst in de plot kunt plaatsen. Je kan tekst toevoegen en die tekst een coördinatielocatie geven of je kan ook tekst toevoegen aan een specifieke plot en misschien een pijl tekenen die rechtstreeks naar die plot verwijst.
as Text
matplotlib.axes.Axes.text(x, y, s, fontdict=None, withdash=False, **kwargs)
Voeg de tekst s
op locatie toe aan de assen (x, y)
in gegevenscoördinaten.
parameters
Naam | Data type | Beschrijving |
---|---|---|
x, y |
scalars |
De positie om de tekst te plaatsen |
s |
str |
De annotatietekst |
fontdict |
dictionary |
Een woordenboek om de standaard eigenschappen van het lettertype te overschrijven |
Assen Text
basisvoorbeeld
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4 * np.pi, 1000)
y = 10 * np.sin(x)
fig, ax = plt.subplots(1, figsize=(6, 4.5))
ax.plot(x, y, "r")
ax.text(2.0, 9.5, "Peak Value",fontsize=14)
ax.grid(True)
plt.show()
Assen Text
Rotatie
De assen text
hebben een trefwoordargument - rotation
die de tekstrotatiehoek in de plot aangeeft. De rotation
hoek ligt tussen 0 en 360 (graden).
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4 * np.pi, 1000)
y = 10 * np.sin(x)
fig, ax = plt.subplots(1, figsize=(6, 4.5))
ax.plot(x, y, "r")
ax.text(1.3, 9.0, "Peak Value", fontsize=16, rotation = 270)
ax.grid(True)
plt.show()
Assen Text
Rotatie Hoek Verklaring
De rotatiehoek is in de richting van tegen de klok in. We maken een demoscript om de definitie van de rotatiehoek te illustreren.
import matplotlib.pyplot as plt
import numpy as np
def addtext(ax, props):
for x in range(8):
angle = 45 * x
ax.text(0.5+x, 0.5, '{} degree'.format(angle), props, rotation=angle)
ax.scatter(x + 0.5, 0.5, color='r')
ax.set_yticks([0, .5, 1])
ax.set_xlim(0, 8)
ax.grid(True)
# the text bounding box
bbox = {'fc': '0.8', 'pad': 0}
fig, axs = plt.subplots(1, 1, figsize=(8, 3))
addtext(axs, {'ha': 'center', 'va': 'center', 'bbox': bbox})
axs.set_xticks(np.arange(0, 8.1, 0.5), [])
axs.set_ylabel('center / center')
plt.show()
De tekst wordt uitgelijnd door het selectiekader dat het rechthoekige vak is dat de tekstrechthoek omgeeft. De tekst wordt eerst gedraaid en vervolgens uitgelijnd. Kortom, de tekst wordt gecentreerd op (x, y)
locatie, rond dit punt geroteerd en vervolgens uitgelijnd volgens het selectiekader van de geroteerde tekst.
Als u dus links, onder uitlijning opgeeft, bevindt de linkeronderhoek van het selectiekader van de geroteerde tekst zich op de (x, y)
-coördinaat van de tekst.
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn