matplotlib 极坐标图作为另一个图中的子图

wljmcqd8  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(202)

我只想在另一个情节里面再策划一个子情节:如您所见,在此代码中,我尝试在其他图中绘制此极坐标图,但无法设置图1的大小和位置:

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

我拥有的

我想要拥有的:

ghhaqwfi

ghhaqwfi1#

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

相关问题