我试着用python画一个二维矩形板。该板将被分成不同数量的部分,每个部分将填充一个阴影图案。此填充图案将具有指定的角度。下面显示了一个有5个截面的矩形,其截面填充方向(以度为单位)为[0,45,0,-45,0]的矩形的示例。它需要能够显示任何方向,而不仅仅是通常的90、45、0,即33、74.5等
你知道我怎么做吗?基本上,我只想显示每个部分的方向,任何其他表达相同结果的方法都会很受欢迎,例如用一条直线代替阴影线。在
编辑(问题回答后):
Greg提供的编辑脚本如下所示。在from numpy import cos, sin
import numpy as np
import matplotlib.pyplot as plt
angles = [0,10,20,30,40,50]
numberOfSections = len(angles)
def plot_hatches(ax, angle, offset=.1):
angle_radians = np.radians(angle)
x = np.linspace(-1, 1, 10)
for c in np.arange(-2, 2, offset):
yprime = cos(angle_radians) * c - sin(angle_radians) * x
xprime = sin(angle_radians) * c + cos(angle_radians) * x
ax.plot(xprime, yprime, color="b", linewidth=2)
ax.set_ylim(0, 1)
ax.set_xlim(0, 1)
return ax
fig, axes = plt.subplots(nrows=1, ncols=numberOfSections, figsize=(16,(16/numberOfSections)), sharex=True, sharey=True)
for i in range(len(axes.flat)):
plot_hatches(axes.flat[i], angles[i])
fig.subplots_adjust(hspace=0, wspace=0)
plt.show()
生成下图。
但在检查时,角度与输入角度不匹配。在