Tracciare frecce verticali per i miei punti
-
21-12-2019 - |
Domanda
Sto cercando di capire un modo per aggiungere una freccia verticale rivolta verso l'alto per ciascuno dei miei punti dati.Ho la trama di dispersione e il codice qui sotto.Ho bisogno delle frecce verticali per iniziare dai punti andare verso l'alto a una lunghezza di circa 0,2 nella scala del grafico.
import matplotlib.pyplot as plt
fig = plt.figure()
a1 = fig.add_subplot(111)
simbh = np.array([5.3, 5.3, 5.5, 5.6, 5.6, 5.8, 5.9, 6.0, 6.2, 6.3, 6.3])
simstel =np.array([10.02, 10.08, 9.64, 9.53, 9.78, 9.65, 10.05, 10.09, 10.08, 10.22, 10.42])
sca2=a1.scatter(simstel, simbh )
.
Soluzione
Gli altri approcci presentati sono fantastici.Vado per il premio più hackerato oggi:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
simbh = np.array([5.3, 5.3, 5.5, 5.6, 5.6, 5.8, 5.9, 6.0, 6.2, 6.3, 6.3])
simstel = np.array([10.02, 10.08, 9.64, 9.53, 9.78, 9.65, 10.05, 10.09, 10.08, 10.22, 10.42])
sca2 = ax.scatter(simstel, simbh)
for x, y in zip(simstel, simbh):
ax.annotate('', xy=(x, y), xytext=(0, 25), textcoords='offset points',
arrowprops=dict(arrowstyle="<|-"))
.
Altri suggerimenti
Questo è un po 'hacky, regolare arrow_offset
e arrow_size
fino a quando la figura sembra giusta.
import matplotlib.pyplot as plt
fig = plt.figure()
a1 = fig.add_subplot(111)
simbh = np.array([5.3, 5.3, 5.5, 5.6, 5.6, 5.8, 5.9, 6.0, 6.2, 6.3, 6.3])
simstel =np.array([10.02, 10.08, 9.64, 9.53, 9.78, 9.65, 10.05, 10.09, 10.08, 10.22, 10.42])
sca2=a1.scatter(simstel, simbh, c='w' )
arrow_offset = 0.08
arrow_size = 500
sca2=a1.scatter(simstel, simbh + arrow_offset,
marker=r'$\uparrow$', s=arrow_size)
. Questo può essere fatto direttamente
from matplotlib import pyplot as plt
import numpy as np
# set up figure
fig, ax = plt.subplots()
# make synthetic data
x = np.linspace(0, 1, 15)
y = np.random.rand(15)
yerr = np.ones_like(x) * .2
# if you are using 1.3.1 or older you might need to use uplims to work
# around a bug, see below
ax.errorbar(x, y, yerr=yerr, lolims=True, ls='none', marker='o')
# adjust axis limits
ax.margins(.1) # margins makes the markers not overlap with the edges
.
C'era una stranezza in che modo queste frecce sono implementate in cui la semantica è cambiata in modo che "Lolims" significhi "il punto dati è il limite inferiore" e "uplims" significa "il punto dati è il valore massimo". Vedi https://github.com/matplotlib/matplotlib/pull/2452
Questo non è super elegante, ma fa il trucco
Per iniziare le frecce al punto dati e salire 0,2 unità:
for x,y in zip(simstel,simbh):
plt.arrow(x,y,0,0.2)
.