title: lexical dispersion-plot with matplotlib
body: <p>I need to plot a graph (of which I am not sure, if it has a specific name: some call it (lexical) "dispersion"-plot (<a href="http://www.nltk.org/book/ch01.html" rel="nofollow noreferrer">NLTK</a>), others "barcode" (<a href="https://matplotlib.org/examples/pylab_examples/barcode_demo.html" rel="nofollow noreferrer">matplotlib</a>)). I have a text, which I split in words and I want the graph to draw a thin line every time the word appears in the text. I want to do this with the <em>matplotlib</em> in <em>python3</em>. (The post "<a href="https://stackoverflow.com/questions/43771135/lexical-dispersion-plot-is-seaborn">Lexical dispersion plot is seaborn</a>" is very similiar to my question, but it uses <code>stripplot</code> from <em>seaborn</em>, but I want to do it with <em>matplotlib</em>.)</p>
  
  <p>I wrote a code, but it needs an unbelievable amount of time to draw it. My question is, how to improve this code or how to make it right in the first place. Here is a MWE:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  text = open("file.txt", "r", encoding="utf-8").read()
  words = re.split("\W", text.lower())
  WORD = "rabbit"
  x = [i for i in range(0,len(words))]
  y = [1 if w == WORD else 0 for w in words]
  fig, ax = plt.subplots()
  ax.bar(x, y, width=0, edgecolor="red")
  ax.set_xticks([])
  ax.set_yticks([])
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/7U8si.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7U8si.png" alt="example dispersion- or barcode-plot"></a></p>