如何在python中绘制填充圆环

时间:2019-05-14 15:12:04

标签: python matplotlib

我正在尝试填充一块圆环。首先,我确定了圆环参数,然后使用for循环将圆环填充到圆环内部,但不会绘制圆环。 如何绘制填充圆环? 我有以下代码:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import animation
from matplotlib import colors as mcolors
import pandas as pd
# making blank image
z=np.zeros((1,300))
x=np.zeros((1,300))
y=np.zeros((1,300))
X, Y = np.meshgrid(x, y)
Z = X+Y
n = 500 
# theta: poloidal angle; phi: toroidal angle 
theta = np.linspace(0, 2*np.pi, n) 
phi   = np.linspace(0, 2*np.pi, n) 
theta, phi = np.meshgrid(theta, phi) 
# R0: major radius; a: minor radius 
R0, a = 50, 20
# torus parametrization 
x1 = (R0 + a*np.cos(theta)) * np.cos(phi) 
y1 = (R0 + a*np.cos(theta)) * np.sin(phi) 
z1 = a * np.sin(theta) 
c = np.full(x1.shape + (4,), [0, 0, 0.85, 1])  # shape (nx, ny, 4)
c[x1>0, -1] = 0 # set these to transparent   0 to pi/2
c[y1>0 , -1] = 0
c[-x1+y1>0 , -1]= 0   #set 0 to pi/4  
print(c.shape)
# filled inside of torus
for x in range(0,256):
    for y in range(0,256):
        for z in range(0,256): 
            for theta in range(0,n):
                for phi in range(0,n):
                    if ( x < (R0 + 50*np.cos(theta)* np.cos(phi))):
                       if (y < (R0 + 50*np.cos(theta) * np.sin(phi))):
                          if (z < (50* np.sin(theta))):
                               c= 1
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
ax.contour3D(X, Y, Z, cmap='binary')
ax.set_xlim([-256, 256])
ax.set_ylim([-256, 256])
ax.set_zlim([-256, 256])
ax.view_init(60, 35)
ax.plot_surface(x1, y1, z1, facecolors=c, rstride=1, cstride=1 )
plt.show()

My result without for loop

0 个答案:

没有答案