1

I want to run this exact code:

import numpy as np
import matplotlib.pyplot as plt

#xs = np.linspace(1, 21, 200)

plt.figure(figsize=(6, 3))
plt.hlines(y='Test1', xmin=1929, xmax=1978, colors='red', linestyles='-', lw=2, label='Test1')
plt.hlines(y='Test2', xmin=1800, xmax=1991, colors='blue', linestyles='-', lw=2, label='Test2')
plt.hlines(y='Test3', xmin=1930, xmax=2007, colors='green', linestyles='-', lw=2, label='Test3')
plt.show()

And get this exact output:

enter image description here

However, I want markers (circles) at either end of each line.

Markers don't seem to be an option in hlines, from the documentation here.

Can someone suggest an alternative to reproducing the same graph, but with markers like these?

Slowat_Kela
  • 1,377
  • 2
  • 22
  • 60

1 Answers1

2

You can achieve the goal using regular plot like the following:

import matplotlib.pyplot as plt

plt.figure(figsize=(6, 3))
plt.plot([1929, 1978], ['Test1', 'Test1'], '-o', color='red')
plt.plot([1800, 1991], ['Test2', 'Test2'], '-o', color='blue')
plt.plot([1930, 2007], ['Test3', 'Test3'], '-o', color='green')

This will result in:

enter image description here

Edit from @LorenaGil:

In case one want's the marker at the end of the line one can use the markevery argument like the following:

 plt.plot([1929, 1978], ['Test1', 'Test1'], '-o', color='red', markevery=[-1])
David
  • 8,113
  • 2
  • 17
  • 36
  • 2
    Moreover, if you only want the markers on the end of the line you can use the parameter markevery, like follows: plt.plot([1929, 1978], ['Test1', 'Test1'], '-o', color='red', markevery=[-1]) – Lorena Gil Nov 03 '20 at 14:59
  • 1
    @LorenaGil True, but the OP said in both ends. But nice remark, I'll add it to the answer. – David Nov 03 '20 at 15:03