-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfPolarPlotFromDataFrame.py
96 lines (65 loc) · 3.2 KB
/
rfPolarPlotFromDataFrame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import matplotlib.pyplot as plt
import mplcursors # module for cursors
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.patches as mpatches
def rfPolarPlotFromDataFrame(dataFrameList, plotUnitaryCircle=False, plotAxis=False, tight = False):
if not isinstance(dataFrameList, list):
raise Exception("The argument is not a list")
# Some Definitions
#Labelfont = 22
#TickLabelfont = 20
LineWd = 2
# Create the plot
Lines = []
fig, ax = plt.subplots(1,figsize=(7, 7))
for item in dataFrameList:
if 'f' not in item.columns:
raise Exception("The data frame does not have the column f")
if 'cplx' not in item.columns:
raise Exception("The data frame does not have the column cplx")
xReal = np.real(item['cplx'].to_numpy())
yImag = np.imag(item['cplx'].to_numpy())
line0, = ax.plot(xReal, yImag,'.')
Lines.append(id(line0))
# Configure the aesthetic pattern the plot
if(tight):
ax.autoscale(enable=True, axis='x', tight=True)
ax.autoscale(enable=True, axis='y', tight=True)
if(plotAxis):
xlim = ax.get_xlim()
ylim = ax.get_ylim()
ax.arrow(xlim[0], 0, xlim[1]-xlim[0], 0, length_includes_head=True, linewidth=0.50, color='k', alpha=0.5, head_length=(xlim[1]-xlim[0])*0.02, head_width=(ylim[1]-ylim[0])*0.015)
ax.arrow(0, ylim[0], 0, ylim[1]-ylim[0], length_includes_head=True, linewidth=0.50, color='k', alpha=0.5, head_length=(ylim[1]-ylim[0])*0.02, head_width=(xlim[1]-xlim[0])*0.015)
if (plotUnitaryCircle):
p = mpatches.Circle((0, 0), 1, alpha=0.1, edgecolor='k', facecolor='g', linestyle='--')
ax.add_patch(p)
# Configure the data cursor
c2 = mplcursors.cursor(multiple=True) # enabling the cursor
@c2.connect("add")
def _(sel):
xi, yi = sel.target
Mag = np.sqrt(xi**2 + yi**2)
Phi = np.arctan2(yi, xi) * 180 / np.pi
# This is a workaround to retrieve the frequency of the point
# It should work if, and only if, the cursor does not point to interpoled values
# Since I'm ploting the curve with '.', instead of '-' and '--', the cursor will only point to values in the input arrays
element = Lines.index(id(sel.artist))
df = dataFrameList[element]
xReal2 = np.real(df['cplx'].to_numpy())
yImag2 = np.imag(df['cplx'].to_numpy())
positionX = np.where(xReal2 == xi)
positionY = np.where(yImag2 == yi)
# A small test to see if everything is working proplerly
# Chances are, it will have conditions not covered by this test
# It is a good idei to check this part in the future
if(positionX[0][0] == positionY[0][0]):
f = df['f'].to_numpy()[positionX[0][0]]
else:
f = -1 # only a flag in case something go wrong
if(yi>0):
sel.annotation.set_text(f"Freq: {f:.2f}Hz\nMag: {Mag:.2f}\n Phi: {Phi:.2f}deg\nValue: {xi:.2f}+j{yi:.2f}")
else:
sel.annotation.set_text(f"Freq: {f:.2f}Hz\nMag: {Mag:.2f}\n Phi: {Phi:.2f}deg\nValue: {xi:.2f}-j{np.abs(yi):.2f}")
plt.show()
return fig, ax