Sine function visualization using Python
Tags: python, math, sine, visualization
Published: 2019-07-12 04:07:23

---
title: Sine function visualization using Python
published: true
description: **"Coding Druid"** series is my **horizontal programming** practice notes, each part around a topic like mathematical, physics, electronic, graphics, sound, etc., implemented in several programming languages.
tags: Python, Math, Sine, visualization
cover_image: https://thepracticaldev.s3.amazonaws.com/i/a4lqcsmxjocphn61ky9l.gif
---


**"Coding Druid"** series is my **horizontal programming** practice notes, each part around a topic like mathematical, physics, electronic, graphics, sound, etc., implemented in several programming languages.

----
**Coding Druid**
Part: Math
Chapter: Trigonometric Function
Section: Python

*In other sections, I visualized Trigonometric function (Sine and unit circle) by [MaxMSP](https://www.patreon.com/posts/sine-function-28131990/), [JavaScript(React)](https://www.patreon.com/posts/sine-function-28178275), [Unity3D](https://www.patreon.com/posts/sine-function-28291611):*


![trig-sine-visualization-unity](https://thepracticaldev.s3.amazonaws.com/i/1lwa8c2fi4f0p4pvcp3n.gif)



This time I use Python to implement it.


For the quick goal, directly Google "python data visualization lib" these keywords, get the lib, sort by the number of stars in github, found the old and strong [Matplotlib](https://matplotlib.org/). So, it was so pleasant to decide.

**Matplotplib Install**

Consider the follow-up exercises, I select the whole-family-bucket installation method provided by [Anaconda](https://www.anaconda.com). It includes a bunch of libraries related to scientific data analysis including Matplotlib.

![Anaconda](https://www.anaconda.com/wp-content/uploads/2018/11/distro-01-1.png)

Due to the recent unfixed working computer and location, I've deployed a remote development environment (VSCode Remote rocks!). The code is deploying and running on the Azure server. I just use a old notebook to edit the code and view the effect. 
So I choose the Linux version for the server environment.

![Anaconda Python 3.0](http://floatcc.intplusplus.org/Anaconda-python3.0.png)

**Note**, the Python 3.x version on the left is preferred here, because Matplotlib 3.0 and above only supports Python 3.

>Matplotlib 3.0 is Python 3 only.
>
>For Python 2 support, Matplotlib 2.2.x will be continued as a LTS release and updated with bugfixes until January 1, 2020.

Then you can start coding after Matplotlib installing.

(In fact, after writing a few lines, I found that remotely developing Python, it is still not convenient to view the result graphics in real time. So I also installed [Jupyter Notebook] (https://jupyter.org/) and coding directly in the browser, WYSIWYG.)

In addition, ffmpeg is installed too, to save the animation obtained by Matplotlib to mp4.
Also used Anaconda to install ffmpeg.
`conda install -c conda-forge ffmpeg`

With Matplotlib, the Sine function animation part, in fact, there is not a few code. Most is actually the coordinate configuration, and draw some auxiliary lines and so on.

![sine python script](http://floatcc.intplusplus.org/sine-py-carbon.png)

Basically coding by according to the tutorial and some reference articles. The characteristics of this library, most of the ideas focus on the algorithmic logic of the mathematical formula itself.

A nice reference article:
[Animations with Matplotlib](https://towardsdatascience.com/animations-with-matplotlib-d96375c5442c)


**Final Effect**:

![sine-visualization-reactjs](https://thepracticaldev.s3.amazonaws.com/i/ov1zpyw286buk9t1glim.gif)


-----


**Talk is cheap. Show me the code!**

This demo of the "Coding Druid" series is open source here:
https://github.com/avantcontra/coding-druid


You can find more resources in my site [floatbug.com](https://www.floatbug.com).
 Or you may **buy me a coffee** in my [Patreon](https://www.patreon.com/avantcontra). Your encouragement is my driving energy!


Cheers~

Contra


- Website: [floatbug.com](https://www.floatbug.com)
- Github: [avantcontra](https://github.com/avantcontra)
- Facebook: [avantcontra](https://facebook.com/avantcontra)
- Twitter: [avantcontra](https://twitter.com/avantcontra)
- Instagram：[avantcontra](https://www.instagram.com/avantcontra)
- Patreon (**buy me a coffee** XD): [avantcontra](https://www.patreon.com/avantcontra)