Matplotlib是Python中令人驚歎的可視化庫,用於數組的二維圖。 Matplotlib是一個基於NumPy數組的多平台數據可視化庫,旨在與更廣泛的SciPy堆棧配合使用。
matplotlib.patches.Circle
這個matplotlib.patches.Circle
類用於在給定的中心創建圓形補丁xy = (x, y)
具有給定的半徑。它使用貝塞爾曲線樣條曲線,並且更接近scale-free圓。
用法: class matplotlib.patches.Circle(xy, radius=5, **kwargs)
parameters:
- xy:它是圓的中心。
- 半徑:它設置要繪製的圓的半徑。其默認值為5個單位,並且是可選的。
下表提供了可選的有效kwargs;
PROPERTY | DESCRIPTION |
---|---|
agg_filter | 一個過濾器函數,它接受一個(m,n,3)浮點數組,一個dpi值返回一個(m,n,3)數組 |
alpha | 浮點數或無 |
animated | bool |
抗鋸齒或抗鋸齒 | unknown |
capstyle | {‘butt’,“回合”,‘projecting’} |
clip_box | Bbox |
clip_on | bool |
clip_path | [(Path,Transform)|補丁|無] |
color | rgba元組的顏色或順序 |
contains | callable |
edgecolor或ec或edgecolors | 顏色或無或‘auto’ |
facecolor或fc或facecolors | 顏色或無 |
figure | figure |
fill | bool |
gid | str |
hatch | {‘/’、‘\’、‘|’、‘-’、‘+’、‘x’, ‘o’、‘O’、‘.’、‘*’} |
in_layout | bool |
joinstyle | {‘miter’,“回合”,‘bevel’} |
線型或ls | {“-”,“-”,“-。”,“:”,“,(偏移量,on-off-seq),...} |
線寬或線寬或lw | 浮點數或無 |
path_effects | AbstractPathEffect |
picker | 無或布爾或浮點數或可贖回 |
path_effects | AbstractPathEffect |
picker | float或callable [[Artist,Event],Tuple [bool,dict]] |
rasterized | 布爾還是無 |
sketch_params | (比例:浮點數,長度:浮點數,隨機性:浮點數) |
snap | 布爾還是無 |
transform | matplotlib.transforms.Transform |
url | str |
visible | bool |
zorder | float |
範例1:
import numpy as np
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib import animation
fig, ax = plt.subplots()
patches = []
# create circles with random sizes
# and locations
N = 12 # number of circles
x = np.random.rand(N)
y = np.random.rand(N)
radii = 0.1 * np.random.rand(N)
for x1, y1, r in zip(x, y, radii):
circle = Circle((x1, y1), r)
patches.append(circle)
# add these circles to a collection
p = PatchCollection(patches, cmap = cm.prism, alpha = 0.4)
ax.add_collection(p)
def animate(i):
# random index to color map
colors = 100 * np.random.rand(len(patches))
# set new color colors
p.set_array(np.array(colors))
return p,
ani = animation.FuncAnimation(fig, animate,
frames = 50, interval = 50)
plt.show()
輸出:
範例2:
import numpy as np
import matplotlib
from matplotlib.patches import Circle, Wedge, Polygon, Ellipse
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
import matplotlib.patches as matpatches
fig, ax = plt.subplots(figsize =(8, 8))
patches = []
circle = Circle((2, 2), 2)
patches.append(circle)
polygon = matpatches.PathPatch(patches[0].get_path())
patches.append(polygon)
colors = 2 * np.random.rand(len(patches))
p = PatchCollection(patches,
cmap = matplotlib.cm.jet,
alpha = 0.4)
p.set_array(np.array(colors))
ax.add_collection(p)
plt.axis([-10, 10, -10, 10])
plt.show()
contain2 = patches[0].get_path().contains_points([[0.5, 0.5],
[1.0, 1.0]])
contain3 = patches[0].contains_point([0.5, 0.5])
contain4 = patches[0].contains_point([1.0, 1.0])
輸出:
相關用法
- Python Matplotlib.ticker.MultipleLocator用法及代碼示例
- Python Matplotlib.gridspec.GridSpec用法及代碼示例
- Python Matplotlib.patches.CirclePolygon用法及代碼示例
- Python Matplotlib.colors.Normalize用法及代碼示例
注:本文由純淨天空篩選整理自RajuKumar19大神的英文原創作品 Matplotlib.patches.Circle class in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。