# In Beijing, you can't see the moon in the Mid Autumn Festival. Draw a blood red moon in Python

A sleeping cat 2021-09-15 07:00:24
beijing moon autumn festival. festival

I'm participating in the Mid Autumn Festival Creative submission competition , Details please see ： Mid Autumn Festival Creative submission contest

hi, Hello everyone , I am a sleeping cat . I don't say much nonsense , Go straight to the code .

``````import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,8))
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([])
# Realization 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 code ``````

The consequence of running code is ： The first step is , We have a python3 Installation package , First install , And then we use pip3 Install and transfer two warehouses .

1. numpy
2. matplotlib

The order to install and transfer the warehouse ：

``````pip3 install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
Copy code ``````

In some places , I don't know what's going on , Downloading the library is very slow , Slow terrible , This will cause a timeout when installing the library . therefore , We use ‘-i pypi.tuna.tsinghua.edu.cn/simple’ , add -i This parameter , We can install... Faster , Don't ask why , Just use it .

After installed , Use command

``````pip3 list
Copy code ``````

see . Here's the picture ： Next , Let's read the code together .

First , Our understanding ·matplotlib· This library .Matplotlib yes Python Drawing library of . It can be connected with NumPy Use it together , Provides an effective MatLab Open source alternatives . To put it bluntly , He is python Of GUI Drawing library ,GUI Drawing libraries are available in many languages , for instance java Medium swing. Want to know more , You can jump to the link below to learn more .‘matplotlib.org/’

Next , Let's get to know ·numpy· This library .NumPy(Numerical Python) yes Python An extended library of language , Support a large number of dimension arrays and matrix operations , In addition, it also provides a large number of mathematical function libraries for array operation .

NumPy The forerunner of Numeric The first is the Jim Hugunin Developed with other collaborators ,2005 year ,Travis Oliphant stay Numeric Another library of the same nature is combined in Numarray The characteristics of , And added other extensions and developed NumPy.NumPy Developed for open source and maintained by many collaborators .

NumPy It's a very fast math library , Mainly used for array calculation , contain ：

• A powerful N Dimensional array object ndarray
• Integrate C/C++/Fortran Code tools
• linear algebra 、 The Fourier transform 、 Random number generation and other functions

I know this matplotlib and numpy These two libraries , Let's talk about how to draw .

First step , Get a canvas first .

``````fig = plt.figure(figsize=(8,8))
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 code ``````

The second step , Use powerful 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 = v * x
y = v * y
Copy code ``````

The third step , Color the circle and show

``````plt.style.use('ggplot')
plt.plot(x, y, color = (0.75,0,0))
plt.show()
Copy code ``````

Of course , This show The method can only be in idea see , Want to save it , We still have a way . Look at the code below ：

``````# Auto save chart
'''
bbox_inches='tight' Cut off the excess blank
'''
plt.savefig('./demo.png',bbox_inches='tight')
plt.show()
Copy code ``````

All right. , All right. , The moon is painted , Let's go to the circle of friends to show off . Just like what you like . Want to learn matplotlib Ku's friend left a message , We can do a column .

https://pythonmana.com/2021/09/20210909124112710A.html