-1

I have a plot that looks like this: enter image description here

I plot those arrows using the annotate method

ax.annotate('sample events', (x_value, y_value), xytext=(x_value, -3000), rotation=90, va='top', arrowprops = {'width': 2, 'headwidth': 4, 'linestyle': '--'})

but what I want is something like: enter image description here

and it's better if the annotation text is on the x-axis, is there any way to do it?

(I've tried ax.axvline(x=x_value) but it makes vertical lines across the whole plot)

Jerry wu
  • 802
  • 8
  • 17
  • Does this answer your question? [Plot a vertical line using matplotlib](https://stackoverflow.com/questions/29096948/plot-a-vertical-line-using-matplotlib) – Trenton McKinney Jul 02 '20 at 02:37

2 Answers2

1

The easy method is to use ax.stem, for example:

import matplotlib.pyplot as plt
import numpy as np

y = np.random.random(20)

fig, ax = plt.subplots()
ax.plot(y)
ax.stem(y, markerfmt='.')
plt.show()

Resulting example plot is:

enter image description here

If You want to hide horizontal line, then use basefmt = " ":

ax.stem(y, basefmt = " ", markerfmt='.')
ipj
  • 3,488
  • 1
  • 14
  • 18
0

At first: You should avoid # within your code, since it will then be interpreted as a comment.

According to the documentation of ax.annotate, you should easily specify the line type as dashed, as well as the label placement. A good list of examples can be found here.

You should try something similar to: ax.annotate('dashed line', xy=(0.25,0.2), xytext=(0.6,0.2),arrowprops={'arrowstyle': '-', 'ls': 'dashed'}, va='center', rotate=90)

moosehead42
  • 411
  • 4
  • 18