多语言展示
当前在线:823今日阅读:19今日分享:20

python 如何用matplotlib画一个漂亮的圆?

图形,有好多图形可以用公式表示,可以用公式表述的图形称之为数学图形。圆是一种基本的数学图形,圆面就是为打上材质。
工具/原料

Anaconda3.exe

方法/步骤
1

首先,奉上圆的公式:

2

接着我们使用matplotlib建立画布:

3

圆面的第一种实现方法:

4

圆面第一种的效果如下:

5

圆面的第二种实现方法:

6

圆面第二种的效果如下:

7

圆面的第三种实现方法:

8

圆面第三种的效果如下:

源码
1

import numpy as npimport matplotlib.pyplot as plt# 该行用于设置chart 的样式,可以注掉plt.style.use('mystyle')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([])# 实现功能theta = np.arange(0, 2 * np.pi + 0.1,2 * np.pi / 1000)x = np.cos(theta)y = np.sin(theta)v = np.linspace(0, 10, 100)v.shape = (100, 1)x = v * xy = v * yplt.plot(x, y, color='red')plt.show()

2

import numpy as npimport matplotlib.pyplot as plt# 该行用于设置chart 的样式,可以注掉plt.style.use('mystyle')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([])# 实现功能theta = np.arange(0, 2 * np.pi, 2 * np.pi / 100)theta = np.append(theta, [2 * np.pi])x = np.cos(theta)y = np.sin(theta)v = np.linspace(0, 10, 100)for r in v:    x1 = r * x    y1 = r * y    plt.plot(x1, y1)plt.show()

3

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib import colors# 该行用于设置chart 的样式,可以注掉plt.style.use('mystyle')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([])theta = np.arange(0, 2 * np.pi, 2 * np.pi / 1000)theta = np.append(theta, [2 * np.pi])v = np.linspace(0, 10, 10)mx = np.max(theta)for tha in theta:    x1 = v * np.cos(tha)    y1 = v * np.sin(tha)    c = tha / mx    plt.plot(x1, y1,color=(x1[0], c, y1[0]))plt.show()END

推荐信息