Home > Software engineering >  A polar plot as a subplot within another plot
A polar plot as a subplot within another plot

Time:12-15

I just want to plot a subplot inside another plot: As you can see, in this code I tried to plot this polar plot inside the other plot, but I can't set the size and place of the plot one:

 from matplotlib.gridspec import GridSpec
time = np.linspace(0, 10, 1000)
height = np.sin(time)
score = time**2   height
distribution = np.random.normal(0, 1, len(time))
fig = plt.figure(figsize=(10, 5))
gs = GridSpec(nrows=1, ncols=2)

ax2 = fig.add_subplot(gs[:, 1])
ax2.plot(time, score)
ax3 = fig.add_subplot(111,polar=True)
ax3.set_theta_zero_location("N")
ax3.plot(0-1.5708, 0,'o', linewidth=2,color="k")
theta1 = np.linspace(-0.48332530310852967-1.5708,0.48332530310852967-1.5708, 100)
ax3.fill_between(theta1, -0.1,  0.3, color='r')
theta2 = np.linspace(0.48332530310852967-1.5708,1.449975909325589-1.5708, 100)
ax3.fill_between(theta2, -0.1,  0.3, color='b')
theta3 = np.linspace(1.449975909325589-1.5708,2.4166265155426485-1.5708, 100)
ax3.fill_between(theta3, -0.1,  0.3, color='r')
theta4 = np.linspace(2.4166265155426485-1.5708,3.3832771217597077-1.5708, 100)
ax3.fill_between(theta4, -0.1,  0.3, color='b')
theta5 = np.linspace(3.3832771217597077-1.5708,3.870223983066126-1.5708, 100)
ax3.fill_between(theta5, -0.1,  0.3, color='g')
theta6 = np.linspace(3.870223983066126-1.5708,4.836874589283185-1.5708, 100)
ax3.fill_between(theta6, -0.1,  0.3, color='r')
theta6 = np.linspace(4.836874589283185-1.5708,5.803525195500244-1.5708, 100)
ax3.fill_between(theta6, -0.1,  0.3, color='b')
#ax3.annotate("{}".format(k), xy=[i, j], fontsize=9)
ax3.set_ylim(-10,1)
#ax3.set_xticks(np.linspace(0,2*np.pi,19)[:-1])
#ax3.set_rlabel_position(-47.5)  # Move radial labels away from plotted line
ax3.grid(True)
plt.show()

what I have enter image description here what I want to have : enter image description here

CodePudding user response:


    from matplotlib.gridspec import GridSpec
    from mpl_toolkits.axes_grid.inset_locator import (inset_axes, InsetPosition,
                                                      mark_inset)
    time = np.linspace(0, 10, 1000)
    height = np.sin(time)
    score = time**2   height
    distribution = np.random.normal(0, 1, len(time))
    fig = plt.figure(figsize=(10, 5))
    gs = GridSpec(nrows=1, ncols=2)
    
    ax2 = fig.add_subplot(gs[:, 0])
    ax2.plot(time, score)
    ax3 = fig.add_axes([0.1, 0.47, 0.25, 0.35], polar=True)
    #ax3 = ax2.inset_axes([0.4,0.4,0.2,0.2], subplot_kw={'projection': 'polar'})
    ax3.set_theta_zero_location("N")
    ax3.plot(0-1.5708, 0,'o', linewidth=2,color="k")
    theta1 = np.linspace(-0.48332530310852967-1.5708,0.48332530310852967-1.5708, 100)
    ax3.fill_between(theta1, -0.1,  0.3, color='r')
    theta2 = np.linspace(0.48332530310852967-1.5708,1.449975909325589-1.5708, 100)
    ax3.fill_between(theta2, -0.1,  0.3, color='b')
    theta3 = np.linspace(1.449975909325589-1.5708,2.4166265155426485-1.5708, 100)
    ax3.fill_between(theta3, -0.1,  0.3, color='r')
    theta4 = np.linspace(2.4166265155426485-1.5708,3.3832771217597077-1.5708, 100)
    ax3.fill_between(theta4, -0.1,  0.3, color='b')
    theta5 = np.linspace(3.3832771217597077-1.5708,3.870223983066126-1.5708, 100)
    ax3.fill_between(theta5, -0.1,  0.3, color='g')
    theta6 = np.linspace(3.870223983066126-1.5708,4.836874589283185-1.5708, 100)
    ax3.fill_between(theta6, -0.1,  0.3, color='r')
    theta6 = np.linspace(4.836874589283185-1.5708,5.803525195500244-1.5708, 100)
    ax3.fill_between(theta6, -0.1,  0.3, color='b')
    #ax3.annotate("{}".format(k), xy=[i, j], fontsize=9)
    ax3.set_ylim(-10,1)
    #ax3.set_xticks(np.linspace(0,2*np.pi,19)[:-1])
    #ax3.set_rlabel_position(-47.5)  # Move radial labels away from plotted line
    ax3.grid(True)
    plt.show()

enter image description here

  • Related