Search code examples
pythondrawingpyx

Drawing braces with Pyx


How can I draw a “braced” line between two arbitrary points with Pyx?

It would look something like this:

Brace example http://tof.canardpc.com/view/d16770a8-0fc6-4e9d-b43c-a11eaa09304d


Solution

  • You can draw pretty braces using sigmoidals. I don't have Pyx installed so I'll just plot these using matplotlib (pylab here). Here beta controls the sharpness of the curves in the braces.

    import numpy as nx
    import pylab as px
    
    
    def half_brace(x, beta):
        x0, x1 = x[0], x[-1]
        y = 1/(1.+nx.exp(-1*beta*(x-x0))) + 1/(1.+nx.exp(-1*beta*(x-x1)))
        return y
    
    xmax, xstep = 20, .01
    xaxis = nx.arange(0, xmax/2, xstep)
    y0 = half_brace(xaxis, 10.)
    y = nx.concatenate((y0, y0[::-1]))
    
    px.plot(nx.arange(0, xmax, xstep), y)
    px.show()
    

    enter image description here

    I plotted this along the x-axis to save screen space, but to get braces along the y-axis just swap x and y. Finally, Pyx has plenty of path drawing functionality built-in which coould also work for your needs.