【问题标题】:How can the line segment be removed within the inner circle and outside the outer circle?怎样去掉内圆内和外圆外的线段?
【发布时间】:2021-04-20 15:10:57
【问题描述】:

如何去除内圆内和外圆外的线段?我编写了以下 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')

谢谢。

【问题讨论】:

    标签: python numpy matplotlib subplot


    【解决方案1】:

    您可以使用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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多