Constellations

Hi! This is my solution for the first project in the Python Visualization skill path. Please let me know if you have any feddback.

1. Set-Up

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

2. Get familiar with real data

# 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

fig = plt.figure(figsize=(4.5,4.5))
fig.add_subplot(1, 1, 1)
plt.scatter(x, y, color="orange", marker ="*")
plt.title("2D Image of Orion Constellation")
plt.xlabel("X Coordinates")
plt.ylabel("Y ooCrdinates")
plt.show()

4. Create a 3d visualization

fig_3d = plt.figure(figsize=(5,5))
fig_3d.add_subplot(1, 1, 1, projection="3d")
constallation3d = plt.scatter(x, y, z, color="blue", marker="*")
plt.title("3d Image of Orion Constellation")
plt.show()

.ipynb file

2 Likes

It’s not a project I’ve attempted myself but the code looks clean and readable and it’s easy to work out what’s going on. :ok_hand:

If you’re hunting for any form of improvements it might be worth getting used to using handles for the axes rather than relying on using plt.title, plt.label etc.
If you have figures that contain multiple axes or you use more than one figure it is generally helpful to hold a reference to every unique figure and axis (in many cases the lines or scatter points you added as well). This helps improve readability and doesn’t rely on constantly remembering what the current figure or axis is (which is what plt.title etc. work on).

It’s possible you’ve not come across this but you can called methods on axis objects which can help make it more obvious what happens in more complex plots. An example of two plots side by side-

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


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

fig = plt.figure(figsize=(10, 5))
ax2d = fig.add_subplot(1, 2, 1)
ax3d = fig.add_subplot(1, 2, 2, projection="3d")

# Plotting/labelling 2D axis
scatter2d = ax2d.scatter(x, y, color="orange", marker="*")
ax2d.set_title("2D Image of Orion Constellation")
ax2d.set_xlabel("X Coordinates")
ax2d.set_ylabel("Y ooCrdinates")

# Plotting/labelling 3D axis
constallation3d = plt.scatter(x, y, z, color="blue", marker="*")
ax3d.set_title("3d Image of Orion Constellation")
plt.show()

1 Like

Hey,
I just made this project and there is something wrong. If you follow the instraction as you did, you will get the 2D plot.

fig_3d = plt.figure(figsize=(5,5))
fig_3d.add_subplot(1, 1, 1, projection="3d")
constallation3d = plt.scatter(x, y, z, color="blue", marker="*")
plt.title("3d Image of Orion Constellation")
plt.show()

Try to use this instead to obtain the 3D:

fig_3d = plt.figure(figsize=(5,5))
constallation3d = fig_3d.add_subplot(1, 1, 1, projection="3d")
constallation3d.scatter(x, y, z, color="blue", marker="*")
plt.title("3d Image of Orion Constellation")
plt.show()

image

1 Like

Having tested both methods I’m not certain why you’d get the 2D plot. The one by @lzimmermannl would provide you with two separate figures, one 2D and one 3D. My version should have two axes on the same figure, one 2D and one 3D.
*As a slight fix to my own code the line-

constallation3d = plt.scatter(x, y, z, color="blue", marker="*")

should be using the axis handle as follows-

constallation3d = ax3d.scatter(x, y, z, color="blue", marker="*")
1 Like

Hey @lzimmermannl

Good job! And congrats! The code is very readable and clean.

Best,
@neuralx