Skip to content Skip to sidebar Skip to footer

How Can I Add Markers On A Bar Graph In Python?

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: def plot_comparison(): lengths = [11380, 44

Solution 1:

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

Post a Comment for "How Can I Add Markers On A Bar Graph In Python?"