3

I have made a horizontal bar graph, now I need to add markers on the bars. How can I do so?

The code I have so far is shown below:

enter image description here

def plot_comparison(): 
lengths = [11380, 44547, 166616, 184373, 193068, 258004, 369582, 462795, 503099, 581158, 660724, 671812, 918449]

y_pos = np.arange(len(length))
error = np.random.rand(len(length))

plt.barh(y_pos, length, xerr=error, align='center', alpha=0.4)
plt.yticks(y_pos, length)
plt.xlabel('Lengths')
plt.title('Comparison of different cuts')
plt.show()
Tonechas
  • 13,398
  • 16
  • 46
  • 80
Sanchit795
  • 51
  • 1
  • 2
  • 4

1 Answers1

4

You can simply add a plot command, plotting the y_pos against the lengths. Make sure to specify a maker and set linestyle to "" (or "none") otherwise the markers will be connected by straight lines.
The following code may be what you're after.

import matplotlib.pyplot as plt
import numpy as np

lengths = [11380, 44547, 166616, 184373, 193068, 258004, 369582, 462795, 503099, 581158, 660724, 671812, 918449]

y_pos = np.arange(len(lengths))
error = np.array(lengths)*0.08

plt.barh(y_pos, lengths, xerr=error, align='center', alpha=0.4)
plt.plot(lengths, y_pos, marker="D", linestyle="", alpha=0.8, color="r")
plt.yticks(y_pos, lengths)
plt.xlabel('Lengths')
plt.title('Comparison of different cuts')
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712