Tracer des flèches verticales pour mes points
-
21-12-2019 - |
Question
J'essaie de trouver un moyen d'ajouter une flèche verticale pointant vers le haut pour chacun de mes points de données.J'ai un nuage de points et un code ci-dessous.J'ai besoin que les flèches verticales partent des points montant jusqu'à une longueur d'environ 0,2 dans l'échelle du graphique.
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 )
La solution
Les autres approches présentées sont excellentes.Je vais pour le prix haussière aujourd'hui:
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="<|-"))
Autres conseils
C'est un peu hacky, ajuste arrow_offset
et arrow_size
jusqu'à ce que le chiffre semble correct.
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)
Ceci peut être fait directement
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
Il y a eu une certaine étrangeté dans la manière dont ces flèches sont implémentées lorsque la sémantique a changé de sorte que "Lolims" signifie "Le point de données" est la limite inférieure "et" Uplims "signifie" Le point de données est la valeur maximale ".
Ce n'est pas super élégant, mais cela fait le tour
Pour que les flèches commencent au point de données et augmentez 0,2 unités:
for x,y in zip(simstel,simbh):
plt.arrow(x,y,0,0.2)