# 3D Solar System Using Matplotlib
The 3D version uses #matplotlib for the animation
Here's the article: **[Simulating a 3D Solar System In Python Using Matplotlib](https://thepythoncodingbook.com/2021/12/11/simulating-3d-solar-system-python-matplotlib/)**
# Using Python to create a solar system
If anyone is looking for a fun exercise to flex their #Python #coding fingers…
Using just gravitational attraction between bodies, you can create your own 2D solar system with as many stars and planets as you want. Here's a binary star system with some relatively stable planets
Here's the article, including a detailed step-by-step tutorial, if you want to read more: **[Simulating Orbiting Planets in a Solar System Using Python](https://thepythoncodingbook.com/2021/09/29/simulating-orbiting-planets-in-a-solar-system-using-python-orbiting-planets-series-1/)**
#Python #simulation #animation
…and there's also a 3D version _(next post)_
@ambv @mariatta but that also means that there will be less quality content on here. Many I’m sure are still focusing mostly on Twitter, so cross-posting primary posts (not RTs), particularly if they don’t have Twitter-specific references should be OK
I realise that those who’ve been here long will resent this, but it’s the price to pay for becoming a little more popular, I guess
They're easy to miss and ignore. But if you've read documentation, you will definitely have seen a * or a / in many function signatures
Now, when you see them next, you'll know perfectly well what they are and what they do
/10
Or maybe you use Matplotlib? Here's `plt.scatter()`
Wait for it
scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)
Did you spot the lone * ?
/9
Maybe you use NumPy a lot? Positional-only and keyword-only arguments appear very often in the NumPy codebase
Here's just one example, NumPy's `mean()` method:
a.mean(axis=None, dtype=None, out=None, keepdims=False, *, where=True)
/8
Let's check:
>>> "hello".replace("l", '-')
'he--o'
>>> "hello".replace(old="l", new='-')
Traceback (most recent call last):
...
TypeError: str.replace() takes no keyword arguments
/7
How about `str.replace()`? You've used that too?
Here's the signature:
replace(self, old, new, count=-1, /)
`old`, `new`, and `count` are all positional-only
/6
>>> numbers = [4, 6, 7, 2]
>>> numbers.sort(reverse=True)
>>> numbers
[7, 6, 4, 2]
All fine here. But try to use a positional argument instead:
>>> numbers.sort(True)
Traceback (most recent call last):
...
TypeError: sort() takes no positional arguments
/4
Have you ever used `list.sort()` with the `key` or `reverse` parameters?
I bet you have!
Here's the signature for `list.sort()`
sort(self, /, *, key=None, reverse=False)
/3
Why do we need them? Are they even ever used?
I bet you've seen them many times and ignored them. I'll go further and bet you've used them, too, without knowing
/2
@jimlawless yes, but I think the classic examples of drawing triangles and squares and so on are _very_ limited in scope and can get boring and childish very quickly.
The module can be used for a lot more than that…
@willmcgugan I know I’m _very_ late to the party, but I’m finally planning to jump in and start creating my first TUI soon
One of the most underrated #Python modules?
**`turtle`**
We know it's used for teaching & not in the "real-world"
But it's usually used only for basic drawings & simple animations for kids–a very limited scope
_We can teach more advanced topics, too…_
Here are some examples…
---
# 1. Data structures: lists, tuples, dictionaries, and sets
Here's an animation that uses all four of these structures:
_[Practise Using Lists, Tuples, Dictionaries, and Sets in Python With the Chaotic Balls Animation](https://thepythoncodingbook.com/2021/10/31/using-lists-tuples-dictionaries-and-sets-in-python/)_
---
# 2. Named Tuples
And a bit further down the line, here's an example of a `turtle` animation of a sunrise that introduces Named Tuples:
_[Sunrise: A Python Turtle Animation using Named Tuples](https://thepythoncodingbook.com/2022/06/12/python-turtle-animation-using-named-tuples-sunrise/)_
---
# 3. Demonstrating real-world processes
Maybe we want to use it model real-world processes in a quick-and-simple way.
Here is a demonstration of "simulating" bouncing balls
There's the single ball version:
_[The Real World in Python: Coding A Bouncing Ball](https://thepythoncodingbook.com/2021/08/19/simulating-a-bouncing-ball-in-python/)_
and the many balls one which deals with **object-oriented programming**:
_[Bouncing Balls Using Object-Oriented Programming in Python](https://thepythoncodingbook.com/2021/09/09/using-object-oriented-programming-in-python-bouncing-balls/)
And, one of my favourites, a simulations of planets orbiting one or more stars. The `turtle` version is in 2D:
_[Simulating Orbiting Planets in a Solar System Using Python](https://thepythoncodingbook.com/2021/09/29/simulating-orbiting-planets-in-a-solar-system-using-python-orbiting-planets-series-1/)_
_There's also a 3D version which uses Matplotlib instead of turtle, but that's off-topic here so I'll post another time_
---
Here are some more ideas from projects I've written up recently
https://thepythoncodingbook.com/category/turtle/
I have loads and loads more which I'll try to write up and publish in the coming months
#animations #python #turtle #coding #programming #LearnToCode #LearnPython
Hi I'm Matt 🙂👋
I'm a senior software developer @ Anaconda. I love #python, #rust & #golang. I'm super passionate about making data science and scientific computing accessible to everyone.
I came into tech after 8 years in the energy industry as an ocean engineer, where I did a whole lot of scientific computing and data science for floating platforms.
Before that, I got a PhD in Naval Architecture and Marine Engineering where I studied hydrodynamics of high-speed ships.
• Rethinking how to teach programming – I prefer the friendly, relaxed approach when communicating about Python programming
• I write about Python on The Python Coding Book blog and on Real Python
• Former Physicist
• Expect posts on scientific and numerical programming –> NumPy, Matplotlib and friends!