简体   繁体   English

如何按角度旋转python / numpy中的一维线图数组?

[英]How to rotate a 1D line graph array in python/numpy by angle?

I'd like to rotate a line graph horizontally.我想水平旋转折线图。 So far, I have the target angle but I'm not able to rotate the graph array (the blue graph in the blot).到目前为止,我有目标角度,但我无法旋转图形阵列(印迹中的蓝色图形)。

import matplotlib.pyplot as plt
import numpy as np

x = [5, 6.5, 7, 8, 6, 5, 3, 4, 3, 0]
y = range(len(x))
best_fit_line = np.poly1d(np.polyfit(y, x, 1))(y)

angle = np.rad2deg(np.arctan2(y[-1] - y[0], best_fit_line[-1] - best_fit_line[0]))
print("angle: " + str(angle))

plt.figure(figsize=(8, 6))
plt.plot(x)
plt.plot(best_fit_line, "--", color="r")
plt.show()

在此处输入图像描述

The target calculations of the array should look like this (please ignore the red line):数组的目标计算应该是这样的(请忽略红线):

在此处输入图像描述

If you have some advice, please let me know.如果你有什么建议,请告诉我。 Thanks.谢谢。

This question is very helpful, in particular the answer by @Mr Tsjolder. 这个问题非常有帮助,尤其是@Mr Tsjolder 的回答。 Adapting that to your question, I had to subtract 90 from the angle you calculated to get the result you want:根据您的问题,我必须从您计算的角度中减去 90 以获得您想要的结果:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import transforms

x = [5, 6.5, 7, 8, 6, 5, 3, 4, 3, 0]
y = range(len(x))
best_fit_line = np.poly1d(np.polyfit(y, x, 1))(y)

angle = np.rad2deg(np.arctan2(y[-1] - y[0], best_fit_line[-1] - best_fit_line[0]))
print("angle: " + str(angle))

plt.figure(figsize=(8, 6))

base = plt.gca().transData
rotation = transforms.Affine2D().rotate_deg(angle - 90)

plt.plot(x, transform = rotation + base)
plt.plot(best_fit_line, "--", color="r", transform = rotation + base)

旋转图


Follow-up question : What if we just need the numerical values of the rotated points?追问:如果我们只需要旋转点的数值怎么办?

Then the matplotlib approach can still be useful.那么 matplotlib 方法仍然有用。 From the rotation object we introduced above, matplotlib can extract the transformation matrix, which we can use to transform any point:从我们上面介绍的rotation object中,matplotlib可以提取出变换矩阵,我们可以用它来变换任意一点:

# extract transformation matrix from the rotation object
M = transforms.Affine2DBase.get_matrix(rotation)[:2, :2]

# example: transform the first point
print((M * [0, 5])[:, 1])

[-2.60096617 4.27024297] [-2.60096617 4.27024297]

The slicing was done to get the dimensions we're interested in, since the rotation happens only in 2D.切片是为了得到我们感兴趣的尺寸,因为旋转只发生在 2D 中。 You can see that the first point from your original data gets transformed to (-2.6, 4.3), agreeing with my plot of the rotated graph above.您可以看到原始数据中的第一个点已转换为 (-2.6, 4.3),这与我上面旋转图的 plot 一致。

In this way you can rotate any point you're interested in, or write a loop to catch them all.通过这种方式,您可以旋转任何您感兴趣的点,或者编写一个循环来捕捉它们。

Arne's awnser is perfect if you like to rotate the graph with matplotlib.如果您想使用 matplotlib 旋转图形,Arne 的 awnser 是完美的选择。 If not, you can take a look a this code:如果没有,你可以看看这段代码:

import matplotlib.pyplot as plt
import numpy as np


def rotate_vector(data, angle):
    # source: 
    # https://datascience.stackexchange.com/questions/57226/how-to-rotate-the-plot-and-find-minimum-point    
    # make rotation matrix
    theta = np.radians(angle)
    co = np.cos(theta)
    si = np.sin(theta)
    rotation_matrix = np.array(((co, -si), (si, co)))
    # rotate data vector
    rotated_vector = data.dot(rotation_matrix)
    return rotated_vector


x = [5, 6.5, 7, 8, 6, 5, 3, 4, 3, 0]
y = range(len(x))
best_fit_line = np.poly1d(np.polyfit(y, x, 1))(y)

angle = np.rad2deg(np.arctan2(y[-1] - y[0], best_fit_line[-1] - best_fit_line[0]))
print("angle:", angle)

# rotate blue line
d = np.hstack((np.vstack(y), np.vstack(x)))
xr = rotate_vector(d, -(angle - 90))

# rotate red line
dd = np.hstack((np.vstack(y), np.vstack(best_fit_line)))
xxr = rotate_vector(dd, -(angle - 90))

plt.figure(figsize=(8, 6))
plt.plot(xr[:, 1]) # or plt.plot(xr[:, 0], xr[:, 1])
plt.plot(xxr[:, 1], "--", color="r")
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM