【问题标题】:Python (3.4.2) How to plot a 2D array with a given radius as a circle?Python(3.4.2)如何将具有给定半径的二维数组绘制为圆?
【发布时间】:2015-02-08 21:06:43
【问题描述】:
  from matplotlib.pyplot import *

例如我有一个二维数组,存储在变量名下:'R':

 ([[ 0.680979  ,  0.8126483 ],
   [ 0.4634487 ,  0.14742914],
   [ 0.27596818,  0.70073533],
   [ 0.52843694,  0.54878972],
   [ 0.13926434,  0.4223568 ],
   [ 0.39956441,  0.31257942],
   [ 0.06566612,  0.65883135],
   [ 0.44879016,  0.33009628],
   [ 0.68340944,  0.67422729],
   [ 0.25075741,  0.08038742]])

我想在以下坐标处绘制一个半径为 r 的圆,例如磁盘 1:

x-coordinate: 0.680979 y-coordinate:0.8126483

我希望将所有圆圈绘制在一张图表上。

【问题讨论】:

    标签: python matplotlib plot radius


    【解决方案1】:

    如果您想以 screen 单位指定圆的大小,请使用 scatter

    import matplotlib.pyplot as plt
    import numpy as np
    
    X, Y = np.array ([[ 0.680979  ,  0.8126483 ],
       [ 0.4634487 ,  0.14742914],
       [ 0.27596818,  0.70073533],
       [ 0.52843694,  0.54878972],
       [ 0.13926434,  0.4223568 ],
       [ 0.39956441,  0.31257942],
       [ 0.06566612,  0.65883135],
       [ 0.44879016,  0.33009628],
       [ 0.68340944,  0.67422729],
       [ 0.25075741,  0.08038742]]).T
    
    r = 10  # in units of sq pixels
    
    fig, ax = plt.subplots()
    sc = ax.scatter(X, Y, s=r)
    

    scatter docs

    如果你想以 data 为单位设置大小:

    import matplotlib.patches as mpatches
    import matplotlib.collections as mcoll
    fig, ax = plt.subplots()
    circles = [mpatches.Circle((x, y), radius=.1) for x, y in zip(X, Y)]
    coll = mcoll.PatchCollection(circles)
    ax.add_collection(coll)
    ax.set_aspect('equal')
    # you may have to set the x and y limits your self.
    ax.set_xlim(...)
    ax.set_ylim(...)
    

    circle doc, PatchCollection doc

    【讨论】:

    • 我收到一个错误,如图,ax = plt.subplots() NameError: name 'plt' is not defined
    猜你喜欢
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 2014-01-12
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多