I am participating in the Mid-Autumn Festival Creative Submission Contest. Please see: Mid-Autumn Festival Creative Submission Contest for details.
Hi, everyone, I’m a sleeping cat. Without further ado, let’s get right to the code.
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8.8))
ax = fig.add_subplot(111)
ax.spines['left'].set_color('none')
ax.spines['bottom'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.set_xticks([])
ax.set_yticks([])
# Implement function
theta = np.arange(0.2* np.pi +0.1.2* np.pi /800)
x = np.cos(theta)
y = np.sin(theta)
v = np.linspace(0.10.80)
v.shape = (80.1)
x = v * x
y = v * y
plt.style.use('ggplot')
plt.plot(x, y, color = (0.75.0.0))
plt.show()
Copy the code
The consequences of running code are:
The first step, we have a python3 installation package, installed first, then we use PIp3 to install two libraries.
- numpy
- matplotlib
The command to install the repository:
pip3 install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
Copy the code
In some places, I don’t know why, the time to download the library is very slow, terrible slow, resulting in the installation of the library will time out. So, we use the ‘-i pypi.tuna.tsinghua.edu.cn/simple, plus -i this parameter, the faster we can install, don’t ask why, just use it.
Once installed, use the command
pip3 list
Copy the code
Look at it. The diagram below:
Next, let’s read the code.
First, let’s look at the matplotlib library. Matplotlib is a drawing library for Python. It can be used in conjunction with NumPy and provides an effective open source alternative to MatLab. In plain English, it is Python’s GUI drawing library, which is found in many languages, such as Swing in Java. To learn more, jump to the link below for more details. ‘matplotlib.org/’
Next, let’s look at the · Numpy · library. NumPy(Numerical Python) is an extension library of Python language, which supports a large number of dimensional array and matrix operations. In addition, it also provides a large number of mathematical function libraries for array operations.
Numeric, the predecessor to NumPy, was originally developed by Jim Hugunin and other collaborators. In 2005, Travis Oliphant combined the features of Numarray, another homogenous library, in Numeric. NumPy was developed by adding other extensions. NumPy is open source and is maintained and developed by many collaborators.
NumPy is a very fast math library for array computation, including:
- A powerful N – dimensional array object nDARray
- Broadcast function
- Tools to integrate C/C++/Fortran code
- Linear algebra, Fourier transform, random number generation, etc
In plain English, numpy is for arithmetic. To learn more, jump to the link below for more details. ‘numpy.org/’
So with matplotlib and Numpy, let’s talk about how to draw them.
The first step is to get a canvas.
FIG = plt.figure(figsize=(8,8)) ax = add_subplot(111) ax.spines['left'].set_color('none') ax.spines['bottom'].set_color('none') ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.set_xticks([]) ax.set_yticks([])Copy the code
Step two, use strong mathematical thinking to make circles.
Theta = np.arange(0,2* np.pi +0.1,2* np.pi /800) x = np.cos(theta) y = np.sin(theta) v = np.linspace(0,10,80) v.shape = (80,1) x is equal to v times x, and y is equal to v times yCopy the code
Step 3: Color and display the circle
plt.style.use('ggplot')
plt.plot(x, y, color = (0.75.0.0))
plt.show()
Copy the code
Of course, this show method can only be seen in idea, we still have a way to save it. Look at the following code:
Plt.savefig ('./demo.png',bbox_inches='tight') plt.show()Copy the code
All right, all right, the moon is done. Let’s show it off on moments. If you like it, give it a thumbs up. If you want to learn matplotlib, we can do a column.