【发布时间】:2019-09-09 19:00:44
【问题描述】:
我正在使用 Python,并且想知道使用任何库的最简单方法是使用图像上定义的点将图像添加到绘图中以连接到绘图上的特定点。
将图片添加到绘图的每个部分以使图片上的红点锚定到该部分的坐标的最简单方法是什么?另外,我需要能够改变图片的方向,即窄部分朝上或朝下。
这是我使用方向向量生成线段的类代码,即 ["TOP", "BOTTOM", "TOP", "TOP", "BOTTOM",...]
class Limb:
delta_L = 0.3
theta = radians(8)
def __init__(self, size=15, length=1):
self.length = length
self.size = size
self.XY = np.zeros((2, size+1))
def build(self, orient_vec):
self.curvature = 0
delta_length = self.length + Limb.delta_L
theta_vec = np.zeros((self.size+1))
if len(orient_vec) != self.size:
exception_string = (
'Orientation vector size must equal limb size.\n' +
'Orientation vector size:\t{}\n'.format(
len(orient_vec)) + 'Limb size:\t{}\n'.format(self.size)
)
raise Exception(exception_string)
else:
for ind, seg in enumerate(orient_vec, 1):
if seg == "TOP":
angle = theta_vec[ind-1] + Limb.theta
self.XY[0, ind] = self.XY[0, ind-1] + \
(delta_length * cos(angle))
self.XY[1, ind] = self.XY[1, ind-1] + \
(delta_length * sin(angle))
self.curvature += Limb.theta
theta_vec[ind] = theta_vec[ind-1] + 2*(Limb.theta)
elif seg == "BOTTOM":
angle = theta_vec[ind-1] - Limb.theta
self.XY[0, ind] = self.XY[0, ind-1] + \
(delta_length * cos(angle))
self.XY[1, ind] = self.XY[1, ind-1] + \
(delta_length * sin(angle))
theta_vec[ind] = theta_vec[ind-1] - 2*(Limb.theta)
self.curvature -= Limb.theta
else:
upto = ind
break
upto = ind
if self.XY.sum() <= 0:
return
else:
self.XY = np.delete(self.XY, np.s_[upto:], 1)
所以要重现情节运行:
from . import Limb
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.ticker import MultipleLocator
import numpy as np
limb = Limb()
orientation_vector = ["TOP", "BOTTOM", "TOP", "TOP",
"BOTTOM", "TOP", "BOTTOM", "TOP", "TOP", "BOTTOM", "TOP", "BOTTOM", "TOP", "TOP", "BOTTOM", ]
limb.build(orientation_vector)
segs = limb.XY.shape[1]
points = limb.XY
fig, ax = plt.subplots()
ax.plot([0, 0], [-2, 2], color='black')
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.plot(points[0, :], points[1, :], color='red')
ax.set_aspect('equal', adjustable='datalim')
plt.show()
【问题讨论】:
标签: python matplotlib plot