Gegeven een plot van signaal in tijdweergave, hoe teken je lijnen die de bijbehorende tijdindex markeren?
Specifiek, gezien een signaalgrafiek met een tijdindex variërend van 0 tot 2.6(s), wil ik verticale rode lijnen tekenen die de corresponderende tijdindex voor de lijst aangeven [0.22058956, 0.33088437, 2.20589566]
, hoe kan ik dat doen?
Antwoord 1, autoriteit 100%
De standaardmanier om verticale lijnen toe te voegen die uw hele plotvenster bedekken zonder dat u hun werkelijke hoogte hoeft op te geven, is plt.axvline
import matplotlib.pyplot as plt
plt.axvline(x=0.22058956)
plt.axvline(x=0.33088437)
plt.axvline(x=2.20589566)
OF
xcoords = [0.22058956, 0.33088437, 2.20589566]
for xc in xcoords:
plt.axvline(x=xc)
U kunt veel van de trefwoorden gebruiken die beschikbaar zijn voor andere plotcommando’s (bijv. color
, linestyle
, linewidth
…). U kunt trefwoordargumenten ymin
en ymax
invoeren als u wilt in assencordinaten (bijv. ymin=0.25
, ymax=0.75
zal de middelste helft van de plot beslaan). Er zijn overeenkomstige functies voor horizontale lijnen (axhline
) en rechthoeken (axvspan
).
Antwoord 2, autoriteit 13%
Voor meerdere lijnen
xposition = [0.3, 0.4, 0.45]
for xc in xposition:
plt.axvline(x=xc, color='k', linestyle='--')
Antwoord 3, autoriteit 9%
Als iemand een legend
en/of colors
wil toevoegen aan enkele verticale lijnen, gebruik dan dit:
import matplotlib.pyplot as plt
# x coordinates for the lines
xcoords = [0.1, 0.3, 0.5]
# colors for the lines
colors = ['r','k','b']
for xc,c in zip(xcoords,colors):
plt.axvline(x=xc, label='line at x = {}'.format(xc), c=c)
plt.legend()
plt.show()
Resultaten:
Antwoord 4, autoriteit 5%
Axvline in een lus aanroepen, zoals anderen hebben gesuggereerd, werkt, maar kan onhandig zijn omdat
- Elke regel is een apart plotobject, waardoor het erg traag gaat als je veel regels hebt.
- Als je de legenda maakt, heeft elke regel een nieuw item, wat misschien niet is wat je wilt.
In plaats daarvan kunt u de volgende gemaksfuncties gebruiken die alle lijnen als een enkel plotobject maken:
import matplotlib.pyplot as plt
import numpy as np
def axhlines(ys, ax=None, lims=None, **plot_kwargs):
"""
Draw horizontal lines across plot
:param ys: A scalar, list, or 1D array of vertical offsets
:param ax: The axis (or none to use gca)
:param lims: Optionally the (xmin, xmax) of the lines
:param plot_kwargs: Keyword arguments to be passed to plot
:return: The plot object corresponding to the lines.
"""
if ax is None:
ax = plt.gca()
ys = np.array((ys, ) if np.isscalar(ys) else ys, copy=False)
if lims is None:
lims = ax.get_xlim()
y_points = np.repeat(ys[:, None], repeats=3, axis=1).flatten()
x_points = np.repeat(np.array(lims + (np.nan, ))[None, :], repeats=len(ys), axis=0).flatten()
plot = ax.plot(x_points, y_points, scalex = False, **plot_kwargs)
return plot
def axvlines(xs, ax=None, lims=None, **plot_kwargs):
"""
Draw vertical lines on plot
:param xs: A scalar, list, or 1D array of horizontal offsets
:param ax: The axis (or none to use gca)
:param lims: Optionally the (ymin, ymax) of the lines
:param plot_kwargs: Keyword arguments to be passed to plot
:return: The plot object corresponding to the lines.
"""
if ax is None:
ax = plt.gca()
xs = np.array((xs, ) if np.isscalar(xs) else xs, copy=False)
if lims is None:
lims = ax.get_ylim()
x_points = np.repeat(xs[:, None], repeats=3, axis=1).flatten()
y_points = np.repeat(np.array(lims + (np.nan, ))[None, :], repeats=len(xs), axis=0).flatten()
plot = ax.plot(x_points, y_points, scaley = False, **plot_kwargs)
return plot
Antwoord 5, autoriteit 5%
matplotlib.pyplot.vlines
versus matplotlib.pyplot.axvline
- Het verschil is dat
vlines
1 of meer locaties accepteert voorx
, terwijlaxvline
één locatie toestaat.- Eén locatie:
x=37
- Meerdere locaties:
x=[37, 38, 39]
- Eén locatie:
vlines
neemtymin
enymax
als een positie op de y-as, terwijlaxvline
ymin
enymax
als een percentage van het bereik van de y-as.- Bij het passeren van meerdere regels naar
vlines
, geef eenlist
toe aanymin
enymax
.
- Bij het passeren van meerdere regels naar
- Als u een figuur plakt met zoiets als
fig, ax = plt.subplots()
, vervang danplt.vlines
ofplt.axvline
METax.vlines
OFax.axvline
, respectievelijk.
import numpy as np
import matplotlib.pyplot as plt
xs = np.linspace(1, 21, 200)
plt.figure(figsize=(10, 7))
# only one line may be specified; full height
plt.axvline(x=36, color='b', label='axvline - full height')
# only one line may be specified; ymin & ymax spedified as a percentage of y-range
plt.axvline(x=36.25, ymin=0.05, ymax=0.95, color='b', label='axvline - % of full height')
# multiple lines all full height
plt.vlines(x=[37, 37.25, 37.5], ymin=0, ymax=len(xs), colors='purple', ls='--', lw=2, label='vline_multiple - full height')
# multiple lines with varying ymin and ymax
plt.vlines(x=[38, 38.25, 38.5], ymin=[0, 25, 75], ymax=[200, 175, 150], colors='teal', ls='--', lw=2, label='vline_multiple - partial height')
# single vline with full ymin and ymax
plt.vlines(x=39, ymin=0, ymax=len(xs), colors='green', ls=':', lw=2, label='vline_single - full height')
# single vline with specific ymin and ymax
plt.vlines(x=39.25, ymin=25, ymax=150, colors='green', ls=':', lw=2, label='vline_single - partial height')
# place legend outside
plt.legend(bbox_to_anchor=(1.0, 1), loc='upper left')
plt.show()
Antwoord 6, Autoriteit 3%
Naast de plt.axvline
en plt.plot((x1, x2), (y1, y2))
OF plt.plot([x1, x2], [y1, y2])
Zoals verstrekt in de bovenstaande antwoorden, kan men ook
gebruiken
plt.vlines(x_pos, ymin=y1, ymax=y2)
Om een verticale lijn op te zetten op x_pos
spanning van y1
naar y2
waarbij de waarden y1
en y2
zijn in absolute gegevenscoördinaten.