python图案填充_用matplotlib用自定义图案填充多边形
以下內容不能解決此問題。它只是解決了問題的一部分,并顯示了該方法在哪一點上失敗。我目前確信matplotlib無法使用任意角度進行圖案填充,因為單位單元格的大小是固定的。
為了克服設置角度的問題,可以定義一個自定義格式,從中獲取角度信息。E、 g."{angle}{factor}",使得{}將產生45°角和密度因子2的陰影。在
然后我不完全理解計算頂點的嘗試。為了復制內置艙口的行為,可以直接旋轉艙口。在
問題是,這種方法只能對45°的角度進行線填充。這是因為單位單元格邊緣的線條沒有很好地對齊。請參見以下內容:import numpy as np
import matplotlib.hatch
import matplotlib.path
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Rectangle
class AngularHatch(matplotlib.hatch.HatchPatternBase):
def __init__(self, hatch, density):
self.num_lines=0
self.num_vertices=0
if hatch[0] == "{":
h = hatch.strip("{}").split("}{")
angle = np.deg2rad(float(h[0])-45)
d = float(h[1])
self.num_lines = int(density*d)
self.num_vertices = (self.num_lines + 1) * 2
self.R = np.array([[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]])
def set_vertices_and_codes(self, vertices, codes):
steps = np.linspace(-0.5, 0.5, self.num_lines + 1, True)
vertices[0::2, 0] = 0.0 + steps
vertices[0::2, 1] = 0.0 - steps
vertices[1::2, 0] = 1.0 + steps
vertices[1::2, 1] = 1.0 - steps
codes[0::2] = matplotlib.path.Path.MOVETO
codes[1::2] = matplotlib.path.Path.LINETO
vertices[:,:] = np.dot((vertices-0.5),self.R)+0.5
matplotlib.hatch._hatch_types.append(AngularHatch)
fig = plt.figure()
ax = fig.add_subplot(111)
ellipse = ax.add_patch(Rectangle((0.1, 0.1), 0.4, 0.8, fill=False))
ellipse.set_hatch('{45}{1}')
ellipse.set_color('red')
ellipse = ax.add_patch(Rectangle((0.55, 0.1), 0.4, 0.8, fill=False))
ellipse.set_hatch('{22}{1}')
ellipse.set_color('blue')
plt.show()
總結
以上是生活随笔為你收集整理的python图案填充_用matplotlib用自定义图案填充多边形的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: **ISO9001好处和定义**
- 下一篇: Python:正则表达式re.compi