 # Visualize Data with Python / Orion Constellation Project

I have recently completed the Orion Constellations path project for the “Visualize Data with Python” skillpath and I encourage any feedback on the project.

Project: Visualizing the Orion Constellation

In this project we will be visualizing the Orion constellation in 2D and 3D using the Matplotlib function .scatter().

The goal of the project is to understand spatial perspective. Once we visualize Orion in both 2D and 3D, we 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

• We will add `%matplotlib notebook` in the cell below. This statement will allow us to be able to rotate out visualization in this jupyter notebook.

• We will be importing `matplotlib.pyplot` as usual.

• 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

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.

``````# 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.

We first create a figure for the 2d plot and save it to a variable name `fig_2d` .

Then we add a subplot `.add_subplot()` as the single subplot, with `1,1,1` .

We use the scatter function to visualize our `x` and `y` coordinates.

We also set the background color to “black” and the marker on the datapoints to “star” so that they imitate the stars in the black sky.

We finally give a title and render our visualization.

The 2D visualization dows not look like the Orion constellation we see in the night sky. 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_2d = plt.figure()
plt.scatter(x,y, color = 'yellow', marker = '*')
plt.title('2D Visualization of the Orion Constellation')
plt.xlabel('Orion x Coordinates')
plt.ylabel('Orion y Coordinates')
ax.set_facecolor('xkcd:black')
plt.show()
`````` 4. Create a 3D Visualization

We first create a figure for the 3D plot and save it to a variable name `fig_3d` .

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

To add a 3D projection, we must include a the projection argument. It would look like this:

``````projection="3d"
``````

Then we add our subplot with `.add_subplot()` as the single subplot `1,1,1` and specify our `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.

We then create a new variable `constellation3d` and call the scatter function with our `x` , `y` and `z` coordinates.

We also set the background color to “black” and the marker on the datapoints to “star” so that they imitate the stars in the black sky.

We finally give a title and render our visualization.

``````fig_3d = plt.figure()
constellation3d.scatter(x,y,z, color = 'yellow', marker = '*', s=50)
plt.title('3D Visualization of the Orion Constellation')
constellation3d.set_xlabel('Orion x Coordinates')
constellation3d.set_ylabel('Orion y Coordinates')
constellation3d.set_zlabel('Orion z Coordinates')
plt.gca().patch.set_facecolor('white')
constellation3d.w_xaxis.set_pane_color((0, 0, 0, 1.0))
constellation3d.w_yaxis.set_pane_color((0, 0, 0, 1.0))
constellation3d.w_zaxis.set_pane_color((0, 0, 0, 1.0))
plt.show()
`````` Any feedback is most welcome. Thanks a lot!

1 Like

I like the colour scheme contrast. I’d advocate for increasing the size of the markers in that second plot by a bit though and I’d suggest having a close look at that final plot from different angles (are those stars all at z=0.0?, I can’t tell for certain but do check). Using the standard `matplotlib.pyplot.plot` function gives you a 2D scatter. You want to use the method of your 3D axis object instead which accepts a third argument of `z`.

2 Likes

You were absolutely right! I was using the wrong object to call the function upon. Now it looks good. And I have also increased the size for the second plot as per your suggestion.

Thanks again!

1 Like