内圈以内和外圈外的线段怎么去除?
How can the line segment be removed within the inner circle and outside the outer circle?
如何去掉内圈和外圈外的线段?我写了下面的 python 代码来绘制两个带有直线 x=0 的圆:
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.rcParams.update({'font.size': 14})
#Plot circle
#Create a list of 500 points with equal spacing between -1 and 1
import numpy as np
x=np.linspace(start=-1,stop=1,num=500)
#Find y1 and y2 for these points
y_positive=lambda x: np.sqrt(1-x**2)
y_negative=lambda x: -np.sqrt(1-x**2)
plt.plot(x,list(map(y_positive, x)), color='maroon')
plt.plot(x,list(map(y_negative, x)),color='maroon')
#Plot smaller circle
x=np.linspace(start=-0.5,stop=0.5,num=500)
y_positive=lambda x: np.sqrt(0.5**2-x**2)
y_negative=lambda x: -np.sqrt(0.5**2-x**2)
plt.plot(x,list(map(y_positive, x)), color='maroon')
plt.plot(x,list(map(y_negative, x)),color='maroon')
#Create broken lines
#x=np.linspace(start=-1,stop=1,num=30)
plt.axvline(0,-1,1,color='maroon')
plt.savefig('circle[enter image description here][1].pdf')
谢谢。
您可以使用 matplotlib.collections
中的 LineCollection
,您的代码将变为:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import collections
import pylab as pl
#Create a list of 500 points with equal spacing between -1 and 1
x=np.linspace(start=-1,stop=1,num=500)
#Find y1 and y2 for these points
y_positive=lambda x: np.sqrt(1-x**2)
y_negative=lambda x: -np.sqrt(1-x**2)
# create figure and get handle
plt.figure(figsize=(10,10))
ax = plt.gca()
plt.rcParams.update({'font.size': 14})
#Plot big circle
ax.plot(x,list(map(y_positive, x)), color='maroon')
ax.plot(x,list(map(y_negative, x)),color='maroon')
x=np.linspace(start=-0.5,stop=0.5,num=500)
y_positive=lambda x: np.sqrt(0.5**2-x**2)
y_negative=lambda x: -np.sqrt(0.5**2-x**2)
#Plot small circle
ax.plot(x,list(map(y_positive, x)), color='maroon')
ax.plot(x,list(map(y_negative, x)),color='maroon')
#Create broken lines
lines = [[(0, 0.5), (0, 1)], [(0, -0.5), (0, -1)]]
lc = collections.LineCollection(lines, color='maroon', linewidths=2)
ax.add_collection(lc)
plt.savefig('circle[enter image description here][1].pdf')
如何去掉内圈和外圈外的线段?我写了下面的 python 代码来绘制两个带有直线 x=0 的圆:
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.rcParams.update({'font.size': 14})
#Plot circle
#Create a list of 500 points with equal spacing between -1 and 1
import numpy as np
x=np.linspace(start=-1,stop=1,num=500)
#Find y1 and y2 for these points
y_positive=lambda x: np.sqrt(1-x**2)
y_negative=lambda x: -np.sqrt(1-x**2)
plt.plot(x,list(map(y_positive, x)), color='maroon')
plt.plot(x,list(map(y_negative, x)),color='maroon')
#Plot smaller circle
x=np.linspace(start=-0.5,stop=0.5,num=500)
y_positive=lambda x: np.sqrt(0.5**2-x**2)
y_negative=lambda x: -np.sqrt(0.5**2-x**2)
plt.plot(x,list(map(y_positive, x)), color='maroon')
plt.plot(x,list(map(y_negative, x)),color='maroon')
#Create broken lines
#x=np.linspace(start=-1,stop=1,num=30)
plt.axvline(0,-1,1,color='maroon')
plt.savefig('circle[enter image description here][1].pdf')
谢谢。
您可以使用 matplotlib.collections
中的 LineCollection
,您的代码将变为:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import collections
import pylab as pl
#Create a list of 500 points with equal spacing between -1 and 1
x=np.linspace(start=-1,stop=1,num=500)
#Find y1 and y2 for these points
y_positive=lambda x: np.sqrt(1-x**2)
y_negative=lambda x: -np.sqrt(1-x**2)
# create figure and get handle
plt.figure(figsize=(10,10))
ax = plt.gca()
plt.rcParams.update({'font.size': 14})
#Plot big circle
ax.plot(x,list(map(y_positive, x)), color='maroon')
ax.plot(x,list(map(y_negative, x)),color='maroon')
x=np.linspace(start=-0.5,stop=0.5,num=500)
y_positive=lambda x: np.sqrt(0.5**2-x**2)
y_negative=lambda x: -np.sqrt(0.5**2-x**2)
#Plot small circle
ax.plot(x,list(map(y_positive, x)), color='maroon')
ax.plot(x,list(map(y_negative, x)),color='maroon')
#Create broken lines
lines = [[(0, 0.5), (0, 1)], [(0, -0.5), (0, -1)]]
lc = collections.LineCollection(lines, color='maroon', linewidths=2)
ax.add_collection(lc)
plt.savefig('circle[enter image description here][1].pdf')