| 1 | """@author: Dhruv Jain, Multi-Body Dynamics Research Group, Purdue University |
| 2 | [email protected]. |
| 3 | |
| 4 | Objective: Plot 3D family of orbits with a colorbar using Plotly |
| 5 | """ |
| 6 | import matplotlibscip-python python matplotlib 3.10.7+dfsg1 matplotlib/__init__: |
| 7 | import plotly.graph_objsscip-python python plotly 5.20.0 `plotly.graph_objs`/__init__: as go |
| 8 | import plotly.ioscip-python python plotly 5.20.0 `plotly.io`/__init__: as pio |
| 9 | |
| 10 | pio.renderers.default = "browser" |
| 11 | |
| 12 | |
| 13 | def plot_orbitsscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().( |
| 14 | muscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(mu), |
| 15 | targeted_po_orbitsscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(targeted_po_orbits), |
| 16 | colourbyscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(colourby), |
| 17 | cb_labelscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(cb_label)="JC", |
| 18 | colourmapscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(colourmap)="plasma", |
| 19 | titlescip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(title)=None, |
| 20 | data_tracescip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(data_trace)=None, |
| 21 | savescip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(save)=False, |
| 22 | ): |
| 23 | """Plot 3D family of orbits with a colorbar |
| 24 | Dhruv Jain, 2 March 2022. |
| 25 | |
| 26 | Parameters |
| 27 | ---------- |
| 28 | mu : float, M2/(M1+M2) |
| 29 | M1 and M2 are mass of Primary Bodies and M2<M1 |
| 30 | targeted_po_orbits : List of dictionary |
| 31 | Targeted Periodic Orbit Information - Dictionary |
| 32 | colourby : list of float |
| 33 | Quantity by which to color orbit family members |
| 34 | cb_label : string, optional |
| 35 | Quantity name for colorbar. The default is 'JC'. |
| 36 | colourmap : colourmap, optional |
| 37 | The default is 'plasma'. |
| 38 | title : string, optional |
| 39 | Title of plot, will also be used as file name if save==True. The default is None. |
| 40 | data_trace : dictionay of plotly.graph_objs object, optional |
| 41 | To add additional traces |
| 42 | save : boolean, optional |
| 43 | Choose if save the plot or not. The default is False. |
| 44 | """ |
| 45 | layout = go.Layoutscip-python python boinor 0.19.0 `plotly.graph_objs._layout`/Layout#(autosize=True) |
| 46 | layout.scene = dictscip-python python python-stdlib 3.11 builtins/dict#( |
| 47 | xaxis=dictscip-python python python-stdlib 3.11 builtins/dict#( |
| 48 | title="x [nd]", |
| 49 | tickfont=dictscip-python python python-stdlib 3.11 builtins/dict#(size=14), |
| 50 | ), |
| 51 | yaxis=dictscip-python python python-stdlib 3.11 builtins/dict#( |
| 52 | title="y [nd]", |
| 53 | tickfont=dictscip-python python python-stdlib 3.11 builtins/dict#(size=14), |
| 54 | ), |
| 55 | zaxis=dictscip-python python python-stdlib 3.11 builtins/dict#( |
| 56 | title="z [nd]", |
| 57 | tickfont=dictscip-python python python-stdlib 3.11 builtins/dict#(size=14), |
| 58 | ), |
| 59 | aspectmode="data", |
| 60 | aspectratio={"x": 1, "y": 1, "z": 1}, |
| 61 | ) |
| 62 | |
| 63 | colour_range = [minscip-python python python-stdlib 3.11 builtins/min().(colourbyscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(colourby)), maxscip-python python python-stdlib 3.11 builtins/max().(colourbyscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(colourby))] |
| 64 | cmap = matplotlib.cm.get_cmapscip-python python matplotlib 3.10.7+dfsg1 matplotlib/__init__:(colourmapscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(colourmap)) |
| 65 | norm = matplotlibscip-python python matplotlib 3.10.7+dfsg1 matplotlib/__init__:.colors.Normalize( |
| 66 | vmin=colour_range[0], vmax=colour_range[1] |
| 67 | ) |
| 68 | colours = [] |
| 69 | for i in colourbyscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(colourby): |
| 70 | colours.appendscip-python python python-stdlib 3.11 builtins/list#append().(cmap(norm(i))) |
| 71 | |
| 72 | fig = go.Figurescip-python python boinor 0.19.0 `plotly.graph_objs._figure`/Figure#(layout=layout) |
| 73 | |
| 74 | for i in rangescip-python python python-stdlib 3.11 builtins/range#(len(targeted_po_orbitsscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(targeted_po_orbits))): |
| 75 | fig.add_trace( |
| 76 | go.Scatter3dscip-python python boinor 0.19.0 `plotly.graph_objs._scatter3d`/Scatter3d#( |
| 77 | x=targeted_po_orbitsscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(targeted_po_orbits)[i]["states"][:, 0], |
| 78 | y=targeted_po_orbitsscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(targeted_po_orbits)[i]["states"][:, 1], |
| 79 | z=targeted_po_orbitsscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(targeted_po_orbits)[i]["states"][:, 2], |
| 80 | mode="lines", |
| 81 | hovertext=f"c={colourbyscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(colourby)[i]:.3f}", |
| 82 | line=go.scatter3d.Linescip-python python boinor 0.19.0 `plotly.graph_objs.scatter3d._line`/Line#(color=f"rgba{colours[i]}", width=5), |
| 83 | ) |
| 84 | ) |
| 85 | |
| 86 | colorbar_trace = go.Scatter3dscip-python python boinor 0.19.0 `plotly.graph_objs._scatter3d`/Scatter3d#( |
| 87 | x=[None], |
| 88 | y=[None], |
| 89 | z=[None], |
| 90 | mode="markers", |
| 91 | marker=dictscip-python python python-stdlib 3.11 builtins/dict#( |
| 92 | colorscale=colourmapscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(colourmap), |
| 93 | showscale=True, |
| 94 | cmin=colour_range[0], |
| 95 | cmax=colour_range[1], |
| 96 | colorbar=dictscip-python python python-stdlib 3.11 builtins/dict#( |
| 97 | title=dictscip-python python python-stdlib 3.11 builtins/dict#(text=cb_labelscip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(cb_label), font=dictscip-python python python-stdlib 3.11 builtins/dict#(size=18)), |
| 98 | thickness=10, |
| 99 | outlinewidth=0, |
| 100 | tickfont=dictscip-python python python-stdlib 3.11 builtins/dict#(size=14), |
| 101 | ), |
| 102 | ), |
| 103 | hoverinfo="none", |
| 104 | ) |
| 105 | fig.add_trace(colorbar_trace) |
| 106 | if data_tracescip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(data_trace) is not None: |
| 107 | for i in rangescip-python python python-stdlib 3.11 builtins/range#(len(data_tracescip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(data_trace))): |
| 108 | fig.add_trace(data_tracescip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(data_trace)[i]) |
| 109 | |
| 110 | fig.update_layout( |
| 111 | title=titlescip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(title), |
| 112 | title_x=0.5, |
| 113 | font=dictscip-python python python-stdlib 3.11 builtins/dict#(size=18), |
| 114 | template="plotly_dark", |
| 115 | showlegend=False, |
| 116 | ) |
| 117 | |
| 118 | if savescip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(save) is True: |
| 119 | fig.write_html(titlescip-python python boinor 0.19.0 `contrib.cr3bp_DhruvJ.cr3bp_po_plot_orbits`/plot_orbits().(title) + ".html") |
| 120 | else: |
| 121 | fig.show() |
| 122 | |
| 123 | return None |