Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot interrupted horizontal lines (segments) in matplotlib in a "cheap way" without using NaNs?

I have to plot several "curves", each one composed by horizontal segments (or even points), using matplotlib library.

A random example of plot. Marker points can be omitted

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^^)

like image 557
iacopo Avatar asked Sep 05 '12 23:09

iacopo


People also ask

How to add a horizontal line across the axis in Matplotlib?

Matplotlib is a popular python library used for plotting, It provides an object-oriented API to render GUI plots The axhline () function in pyplot module of matplotlib library is used to add a horizontal line across the axis. y: Position on Y axis to plot the line, It accepts integers.

What is a disconnected line segment in Matplotlib?

Also, color is an rgb with alpha value. In general, any two line segments are disconnected (meaning that their end-points do not necessarily coincide). How to plot this data using matplotlib with a single plot call (or as few as possible) as there could be potentially thousands of records.

How to draw multiple lines in Matplotlib?

To draw multiple lines we will use different functions which are as follows: y = x x = y y = sin(x) y = cos(x) Python3 # importing package importmatplotlib.pyplot as plt importnumpy as np # create data x =[1,2,3,4,5] y =[3,3,3,3,3] # plot lines plt.plot(x, y, label ="line 1") plt.plot(y, x, label ="line 2")

How to create error bars and grid lines in Matplotlib?

plt.errorbar () method is used to plot error bars and we pass the argument x, y, and xerr and set the value of xerr = 1.2. Then we use plt.show () method to display the error bar plotted graph. In matplotlib, the grid () method is used to create grid lines. The syntax to create horizontal grid lines:


1 Answers

Is this something like what you wish to achieve?

import matplotlib.pyplot as plt

segments = {1: [(0, 500),
                (915, 1000)],
            2: [(0, 250),
                (500, 1000)]}

colors = {1: 'b', 2: 'r'}

for y in segments:
    col = colors.get(y, 'k')
    for seg in segments[y]:
        plt.plot(seg, [y, y], color=col)

I'm just defining the y values as keys and a list of line segments (xlo, xhi) to be plotted at each y value.

like image 183
Taro Sato Avatar answered Sep 24 '22 08:09

Taro Sato