Visualizing the Orion Constellation

I have something different from others!
It showed an error when I code"%matplotlib notebook".
And I search google, I found I haven’t installed the ipympl yet.
So I installed it and change the code to “%matplotlib widget”, it’s done!
here the line: https://github.com/matplotlib/ipympl

First

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

Second

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

Third
I have a question.
Why I have to code"fig.add_subplot"
It “plt.add_subplot” doesn’t work?
Why? Is it because “add_subplot” is kind of attribute?

fig = plt.figure()
fig.add_subplot(1,1,1)
plt.scatter(x,y)
plt.show()

Fourth

fig_3d = plt.figure()

fig_3d.add_subplot(1,1,1,projection="3d")
constellation3d = plt.scatter(x,y,z,color="black")
plt.show
1 Like

Good stuff.
In regards to your question, add_subplot is a method of your figure instance. If you’ve not spent much time working with classes then this could be confusing and you may need to do some background reading or lessons on how they work. If you have then you’ll need to spend a little time nosing around how matplotlib woks under the hood since it’s not immediately obvious (or at least it wasn’t when I first started using it).

There are ways to create subplots without having a figure instance in the first place since one is created with the function call anyway, e.g. fig1, ax1 = plt.subplots(1, 1)

Thanks for your help!

If you got a javascript-related error when using %matplotlib notebook, you’re probably using JupyterLab - there’s a known issue there.

Use the base Jupyter Notebooks instead or %matplotlib inline.

1 Like