Wapiti
Wapiti

Reputation: 1911

How to get circles into correct subplot in matplotlib?

I am trying to recreate this https://www.youtube.com/watch?v=LznjC4Lo7lE

I am able to plot the circles I want in matplotlib, but I want to have two subplots next to each other with the circles in the left subplot. I guess I don't understand the ax1,ax2 variables, because the lines wind up in the left subplot, and the circles in the right subplot.

I need them both in the left subplot so I can put the sinusoids in the right subplot. I think my mistake is simple but trying ax1.Circle crashes because Circle is only callable from plt. Any ideas?

Here is the code:

def harmonics( Branches, colors, t, dotsize=2, blur=True, circles=True,save=False, newdir=False, prefix='',path='',note=''):
    txt=''
    Ds=[]
    f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
#    ax1.plot(x, y)
#    ax1.set_title('Sharing Y axis')
#    ax2.scatter(x, y)

    for ct2, B in enumerate(Branches): 
        D,P,L,K,N = B[0],B[1],B[2],B[3],B[4]
        Ds.append(sum([np.absolute(val) for val in D]))
        col = colors[ct2]
        A = ramify(B,t)
        R = [0+0j]

        coll=np.array([])
        fig = plt.gcf()
        KIRKULOS={}

        for ct,a in enumerate(A):
            for x in range(len(a)):
                if x == 0:
                    ax1.plot([R[ct].real,float(a[x].real)+R[ct].real],[R[ct].imag,float(a[x].imag)+R[ct].imag],col,ms=dotsize+2)#color='#FF4D4D',label='python')
                    rr = float(a[x].real)+R[ct].real + (float(a[x].imag)+R[ct].imag)*1j 
                    R.append(rr)
                else:
                    pass#plt.plot([R[ct].real,float(a[x].real)+R[ct].real],[R[ct].imag,float(a[x].imag)+R[ct].imag],color='#FFFFFF',ms=dotsize,label='python')
            if circles:
                KIRKULOS[ct]=plt.Circle((R[ct].real,R[ct].imag),D[ct],color=col[-1],  fill=False)    
                plt.gca().add_artist(KIRKULOS[ct])
            coll = np.append(coll,a)

    plt.axis("equal")
    limit=max(Ds)+max(Ds)*.2
    plt.xlim((-limit,limit))
    plt.ylim((-limit,limit))
    plt.ylabel('iy')
    plt.xlabel('x')

    for ct,B in enumerate(Branches):# in RAMI.keys():
        D,P,L,K,N = B[0],B[1],B[2],B[3],B[4]
        txt = txt +'\n\n'+str(D)+'\t Radius'+'\n'+str(P)+'\t Phase'+'\n'+str(L)+'\t Order'+'\n'+str(K)+'\t Frequency'+'\n'+str(N)+'\t Degree'
    txt=txt+'\n\n'+note
    fig.set_size_inches(10,10)
    if save:
        if newdir:
            import time
            import os
            now = str(time.time())+'/'
            os.makedirs(path+now)
            path = path+now
        f=open(path+prefix+'.txt','wb+')
        f.write(txt)
        f.close()
        plt.savefig(path+prefix+'.jpg')
    plt.show() 

Upvotes: 1

Views: 1320

Answers (1)

tillsten
tillsten

Reputation: 14878

Circle just generates the patch, calling it does not add the cirlce to an subplot (axes). This is than done plt.gca().add_artist(KIRKULOS[ct]). gca stands for get current axes and return the active subplot, so replacing it with your ax will do: ax1.add_artist(KIRKULOS[ct])

Upvotes: 1

Related Questions