sci-guy
sci-guy

Reputation: 2584

Plot each number array element as a horizontal line

Is there an easy way to plot each array element as a horizontal line in python?

Example:

some_array = [2 4 5 8 4 3 ... n]

Now I want a graph that plots the horizontal lines:

y_1 = 2
y_2 = 4
y_3 = 5
.
.
.
y_n = n

The length of some_array is not fixed, as in I don't know how many elements it will contain, so the code has to be able to handle this.

I know I can do this with :

plt.plot((x[0], x[-1]), (some_array[0], some_array[0]), 'k-')

and maybe put it in a while loop so it runs the length of the array, but I feel like there is a better way.

Upvotes: 2

Views: 1245

Answers (1)

Stop harming Monica
Stop harming Monica

Reputation: 12590

Use plt.hlines:

some_array = [2, 4, 5, 8, 4, 3]
plt.hlines(some_array, 0, 1, colors=['b', 'g', 'r', 'y', 'c', 'm'])
plt.ylim(1, 9)

hlines.png

Upvotes: 6

Related Questions