Orion Constellation (Python)

Codecademy project

Here is the whole code:

get_ipython().run_line_magic('matplotlib', 'notebook')
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = [-0.41, 0.57, 0.07, 0.00, -0.29, -0.32,-0.50,-0.23, -0.23]
y = [4.12, 7.71, 2.36, 9.10, 13.35, 8.13, 7.19, 13.25,13.43]
z = [2.06, 0.84, 1.56, 2.07, 2.36, 1.72, 0.66, 1.25,1.38]

fig = plt.figure()

fig_3d = plt.figure()

The 3D plot creates this:

My question from here

The project instruction says this plot does not look like what we see from the earth because the plot space isn’t curved.
Is there a way to implement the real-sky curve?

Thank you!

I can see it being a little tricky as the 3D plotting options in matplotlib are pretty limited, it is not the best package for that kind of thing so you mat want to look into alternatives if it interests you.

You could check the docs for the details on swapping the view between perspective and orthographic so that it appeared as an effective 3D view (though I wonder if single points will still look a bit weird, only one way to find out). The relevant sections is linked below .set_proj_type method in case it doesn’t pop up immediately-

For the view from Earth you’d need to be adjusting the view location which is controlled by the view_init method- https://matplotlib.org/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.html#mpl_toolkits.mplot3d.axes3d.Axes3D.view_init

You may have to play around with the values to get the correct view and perhaps you’d even have to fiddle about with the effective scales too. If there’s a proper way to do it in matplotlib I’m unaware of it but perhaps try searching online as I’m sure someone will have tried it at some point.

For a little feedback on the code itself please consider adding labels and titles to the plot. It’s a good habit to get into as your figure should be providing as much information as possible to the viewer without them needing to read through a wall of text.

There’s also a an issue with using the standard matplotlib.pyplot.scatter function which is a 2D scatter function (all those points are on the same xy plane at z=0.0 ). You need to be using the scatter method of the 3d axis itself, e.g. ax3d.scatter(x, y, z ...). See the docs for info-

1 Like

Thank you so much, this is a wonderful feedback. Learning a lot more here!

Can I possibly ask one more thing, is there any other way to have an interactive (adjustable) view of the plot than to use Jupyter? I was wondering if I could for example include an interactive plot to a webpage or send to someone who doesn’t necessarily want to open on Jupyter…

1 Like

Web development is a bit of a blind spot for me so I’m none too sure of this one. You may have to hunt it down yourself. Some degree of interactive image is possible but how easy it is to transfer from matplotlib I don’t know. Personally I’d make use of matplot.animation which let you build videos/animations on a frame by frame basis so you could get a .gif, .mp4 etc. etc. for upload but that’d be because I’m unaware of how to make it interactive on a webpage.

If you’re just sending it to a friend/colleague etc. then unless the dataset is ridiculous it may be easier to just send the script. maptlotlib has several interactive backends and even the basic interactive prompt provided by invoking python in the command line is capable of interactive plots- https://matplotlib.org/3.3.2/users/interactive.html
They would still need Python and instsllation of one these backends though, once they have Python though it is very straightforward to get the backend sorted as there are python packages for most of them (as easy as using pip).
Perhaps there’s a simpler route than this so all you can do is check.

1 Like