I have to plot several "curves", each one composed by horizontal segments (or even points), using matplotlib library.
I reached this goal separing the segments by NaNs. This is my example (working) code:
from pylab import arange, randint, hold, plot, show, nan, ylim, legend
n = 6
L = 25
hold(True)
for i in range(n):
x = arange(L, dtype=float) # generates a 1xL array of floats
m = randint(1, L)
x[randint(1, L, m)] = nan # set m values as NaN
y = [n - i] * len(x) # constant y value
plot(x, y, '.-')
leg = ['data_{}'.format(j+1) for j in range(n)]
legend(leg)
ylim(0, i + 2)
show()
(actually, I start from lists of integers: NaNs are added after where integers are missing)
Problem: since each line requires an array of length L, this solution can be expensive in terms of memory if L is big, while the necessary and sufficient information are the limits of segments.
For example, for one line composed by 2 segments of limits (0, 500) and (915, 62000) it would be nice to do something like this:
niceplot([(0, 500), (915, 62000)], [(1, 1), (1, 1)])
(note: this - with plot instead niceplot... - is a working code but it makes other things...)
4*2 values instead of 62000*2... Any suggestions?
(this is my first question, be clement^^)
hlines
function. – Hilarius