# Orion constellation project

I have completed the Orion Constellations path project for the “Visualize Data with Python” skillpath and I’m hoping for your feedback on the project.

## Project: Visualizing the Orion Constellation

In this project you are Dr. Jillian Bellovary, a real-life astronomer for the Hayden Planetarium at the American Museum of Natural History. As an astronomer, part of your job is to study the stars. You’ve recently become interested in the constellation Orion, a collection of stars that appear in our night sky and form the shape of Orion, a warrior God from ancient Greek mythology.

As a researcher on the Hayden Planetarium team, you are in charge of visualizing the Orion constellation in 3D using the Matplotlib function `.scatter()`. To learn more about the `.scatter()` you can see the Matplotlib documentation here.

You will create a rotate-able visualization of the position of the Orion’s stars and get a better sense of their actual positions. To achieve this, you will be mapping real data from outer space that maps the position of the stars in the sky

The goal of the project is to understand spatial perspective. Once you visualize Orion in both 2D and 3D, you will be able to see the difference in the constellation shape humans see from earth versus the actual position of the stars that make up this constellation.

## 1. Set-Up

The following set-up is new and specific to the project. It is very similar to the way you have imported Matplotlib in previous lessons.

• Add `%matplotlib notebook` in the cell below. This is a new statement that you may not have seen before. It will allow you to be able to rotate your visualization in this jupyter notebook.

• We will be using a subset of Matplotlib: `matplotlib.pyplot`. Import the subset as you have been importing it in previous lessons: `from matplotlib import pyplot as plt`

• In order to see our 3D visualization, we also need to add this new line after we import Matplotlib:
`from mpl_toolkits.mplot3d import Axes3D`

``````%matplotlib notebook
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
``````

## 2. Get familiar with real data

Astronomers describe a star’s position in the sky by using a pair of angles: declination and right ascension. Declination is similar to longitude, but it is projected on the celestian fear. Right ascension is known as the ‘hour angle’ because it accounts for time of day and earth’s rotaiton. Both angles are relative to the celestial equator. You can learn more about star position here.

The `x`, `y`, and `z` lists below are composed of the x, y, z coordinates for each star in the collection of stars that make up the Orion constellation as documented in a paper by Nottingham Trent Univesity on ‘The Orion constellation as an installation’ found here.

Spend some time looking at `x`, `y`, and `z`, does each fall within a range?

``````# Orion
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]
``````

## 3. Create a 2D Visualization

Before we visualize the stars in 3D, let’s get a sense of what they look like in 2D.

Create a figure for the 2d plot and save it to a variable name `fig`. (hint: `plt.figure()`)

Add your subplot `.add_subplot()` as the single subplot, with `1,1,1`.(hint: `add_subplot(1,1,1)`)

Use the scatter function to visualize your `x` and `y` coordinates. (hint: `.scatter(x,y)`)

Render your visualization. (hint: `plt.show()`)

Does the 2D visualization look like the Orion constellation we see in the night sky? Do you recognize its shape in 2D? There is a curve to the sky, and this is a flat visualization, but we will visualize it in 3D in the next step to get a better sense of the actual star positions.

``````fig_orion_2d = plt.figure()

plt.scatter(x,y,color='yellow',marker='*')

ax_orion_2d.set_facecolor('black')
plt.title('2D Visualization of Orion')
plt.xlabel('Orion x Coordinates')
plt.ylabel('Orion y Coordinates')

plt.show()
``````

## 4. Create a 3D Visualization

Create a figure for the 3D plot and save it to a variable name `fig_3d`. (hint: `plt.figure()`)

Since this will be a 3D projection, we want to make to tell Matplotlib this will be a 3D plot.

To add a 3D projection, you must include a the projection argument. It would look like this:
`projection='3d'`

Add your subplot with `.add_subplot()` as the single subplot `1,1,1` and specify your `projection` as `3d`:

`fig_3d.add_subplot(1,1,1,projection='3d')`)

Since this visualization will be in 3D, we will need our third dimension. In this case, our `z` coordinate.

Create a new variable `constellation3d` and call the scatter function with your `x`, `y` and `z` coordinates.

Include `z` just as you have been including the other two axes. (hint: `.scatter(x,y,z)`)

Render your visualization. (hint `plt.show()`.)

``````fig_orion_3d = plt.figure()
ax_orion_3d.scatter(x,y,z,color='yellow',marker='*',s=100)

plt.title('3D Visualization of Orion')
ax_orion_3d.set_xlabel('Orion x Coordinates')
ax_orion_3d.set_ylabel('Orion y Coordinates')
ax_orion_3d.set_zlabel('Orion z Coordinates')

ax_orion_3d.xaxis.set_pane_color('black')
ax_orion_3d.yaxis.set_pane_color('black')
ax_orion_3d.zaxis.set_pane_color('black')

plt.show()
``````

## 5. Rotate and explore

Use your mouse to click and drag the 3D visualization in the previous step. This will rotate the scatter plot. As you rotate, can you see Orion from different angles?

Note: The on and off button that appears above the 3D scatter plot allows you to toggle rotation of your 3D visualization in your notebook.

Take your time, rotate around! Remember, this will never look exactly like the Orion we see from Earth. The visualization does not curve as the night sky does.
There is beauty in the new understanding of Earthly perspective! We see the shape of the warrior Orion because of Earth’s location in the universe and the location of the stars in that constellation.

Feel free to map more stars by looking up other celestial x, y, z coordinates here.

``````# Five nearest stars to Earth
nearest5_x = [0, -1.643, -0.0566, -7.416, -6.523]
nearest5_y = [0, -1.374, -5.920, 2.193, 1.646]
nearest5_z = [0, -3.838, 0.486, 0.993, 4.882]

fig_nearest5_3d = plt.figure()
ax_nearest5_3d.scatter(nearest5_x,nearest5_y,nearest5_z,color='yellow',marker='*',s=100)

plt.title('3D Visualization of the five nearest stars to Earth')
ax_nearest5_3d.set_xlabel('x Coordinates')
ax_nearest5_3d.set_ylabel('y Coordinates')
ax_nearest5_3d.set_zlabel('z Coordinates')

ax_nearest5_3d.xaxis.set_pane_color('black')
ax_nearest5_3d.yaxis.set_pane_color('black')
ax_nearest5_3d.zaxis.set_pane_color('black')

plt.show()
``````

Figure 1: Figure 2: Figure 3: Maybe you’ll find this one interesting: After finishing the project, I started to play around with ChatGPT, finally getting out this piece of code for another visualization:

``````# Create an array of the x, y, and z coordinates of the stars in Ursa Major
x = [279.23473479, 283.81500244, 286.4960327, 301.08043823, 304.04800415, 310.05000305, 312.68200684, 320.36300659, 323.45300293]
y = [43.05854797, 39.83827591, 37.12974930, 36.65134048, 34.94216919, 33.65153503, 31.90245819, 29.09178925, 26.51992416]
z = [79.78, 83.24, 87.93, 97.99, 103.31, 128.91, 135.19, 160.43, 165.58]

# Create an array with the names of the stars
names = ['Alkaid', 'Mizar', 'Alioth', 'Megrez', 'Phecda', 'Merak', 'Dubhe', 'Alcor', 'Alnilam']

# Create the figure and 3D scatter plot
fig = plt.figure()
ax.set_xlim(min(x), max(x))
ax.set_ylim(min(y), max(y))
ax.set_zlim(min(z), max(z))
ax.scatter(x, y, z, c='y', marker='*', s=200)

# Iterate over the x, y, z coordinates and names of the stars
for xi, yi, zi, name in zip(x, y, z, names):
# Annotate the marker with the name of the star
ax.text(xi, yi, zi, name, color='w')

# Set the x, y, and z axis labels
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# Set the color of the panes to black
ax.xaxis.set_pane_color((0.0, 0.0, 0.0, 1.0))
ax.yaxis.set_pane_color((0.0, 0.0, 0.0, 1.0))
ax.zaxis.set_pane_color((0.0, 0.0, 0.0, 1.0))

# Set the title of the plot
ax.set_title('Stars in Ursa Major')

# Set the tick labels to whole numbers
ax.xaxis.set_major_formatter(plt.FormatStrFormatter('%d'))
ax.yaxis.set_major_formatter(plt.FormatStrFormatter('%d'))
ax.zaxis.set_major_formatter(plt.FormatStrFormatter('%d'))

# Show the plot
plt.show()
`````` Hey friend, I hope you are doing great!

Congrats on your work, impressive and creative. I was trying to get ideas from you but I have been stuck here and I can not figure out why. Maybe you can help. I use even exactly your code but the plot doesn’t appear. I work on VSCode, do you think that this is an issue?

Did you follow the first two steps (Importing the moduls and setting the variables) - without doing this the third step alone can’t work.

1 Like

Yes, I did that. It seems that apparently, I had to update the ipykernel in my Anaconda environment in order for the code to work. Thanks for your reply friend.

P.S. I am currently building my portfolio with small end-to-end projects, if you have something in mind, could you please suggest any? Thanks in advance.