简体   繁体   English

绘制多个图形并将圆添加到绘图中

[英]Plotting Multiple figures and adding circles to plots

I'm currently coding the trajectories of orbits and am trying to plot two figures during one run. 我目前正在编码轨道轨迹,并试图在一次运行中绘制两个数字。 I want one figure to display the orbital path of a satellite with the earth and moon visible on the plot as circles. 我想用一个图来显示卫星的轨道路径,并在图中以圆的形式显示地球和月球。 I then want a separate figure that displays the energies of the trajectory with time. 然后,我想要一个单独的图形,该图形显示时间的轨迹能量。

I think the problem I'm having lies with trying to add circles to the first figure, though I'm at a complete loss on how to circumvent this issue. 我认为我遇到的问题在于尝试向第一个数字添加圆圈,尽管我对如何规避此问题完全迷失了。 Re and Rm are just the radius of the earth and moon respectively and the lists are just the lists of my values from the rest of the code. Re和Rm分别只是地球和月球的半径,列表仅是其余代码中我的值的列表。 I think the problem is in the fig,axes = plt.subplots line. 我认为问题出在无花果上,axes = plt.subplots行。 Instead of getting two separate figures I just get all the values mashed into one figure. 我没有得到两个单独的数字,而是将所有值混搭为一个数字。

plt.figure(1)
fig, axes = plt.subplots()
earth = plt.Circle((0,0), Re, color = 'blue' )
moon = plt.Circle((0,ym), Rm, color = 'yellow' )
plt.gca().set_aspect('equal', adjustable='box')
plt.plot(xlist,ylist)
axes.add_patch(earth)
axes.add_patch(moon)

plt.figure(2)
plt.plot(tlist,pelist, 'r')
plt.plot(tlist,kelist, 'b')
plt.plot(tlist,elist, 'g')

plt.show()

The second figure does not have a subplot to which plt.plot() could plot. 第二个图没有plt.plot()可以绘制到的子图。 It therefore takes the last open subplot (axes). 因此,它将采用最后一个打开的子图(轴)。 There is also one figure more than needed in the game. 游戏中还需要一个数字。

The easy solution is to delete the double figure creation and add a subplot to the second figure: 一种简单的解决方案是删除双重图形创建,并向第二个图形添加一个子图:

import matplotlib.pyplot as plt

fig, axes = plt.subplots()
earth = plt.Circle((0,0),1, color = 'blue' )
moon = plt.Circle((0,2),0.2, color = 'yellow' )
plt.gca().set_aspect('equal', adjustable='box')
axes.add_patch(earth)
axes.add_patch(moon)
plt.plot([0,0],[0,2])

plt.figure(2)
plt.subplot(111)
plt.plot([1,2,3],[3,2,1], 'r')
plt.plot([1,2,3],[2,3,1], 'b')
plt.plot([1,2,3],[1,3,2], 'g')

plt.show()

A better solution which makes it also more comprehensible is to use only the creted objects for plotting (matplotlib API). 一种更好的解决方案,也使其更易于理解,是仅使用带纹理的对象进行绘图(matplotlib API)。

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.set_aspect('equal', adjustable='box')
earth = plt.Circle((0,0),1, color = 'blue' )
moon = plt.Circle((0,2),0.2, color = 'yellow' )
ax.add_patch(earth)
ax.add_patch(moon)
ax.plot([0,0],[0,2])

fig2, ax2 = plt.subplots()
ax2.plot([1,2,3],[3,2,1], 'r')
ax2.plot([1,2,3],[2,3,1], 'b')
ax2.plot([1,2,3],[1,3,2], 'g')

plt.show()

In this case it's unambiguous to which axes the plots belong. 在这种情况下,曲线所属的轴是明确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM