nichollsg
nichollsg

Reputation: 390

python plotting data marker

I am trying to make a data marker on a python plot that shows the x and y coordinates, preferably automatically if this is possible. Please keep in mind that I am new to python and do not have any experience using the marker functionality in matplotlib. I have FFT plots from .csv files that I am trying to compare to theoretical calculations, but I need a way of highlighting a specific point and dropping a marker that has the coordinate values similar to MATLAB. For reference, I am plotting an FFT of frequency intensity of a 100kHz sine wave with an amplitude of 1V, so I am trying to show that the spike at 100kHz is close to the calculated value of 3.98dBm in a 50ohm environment. Here is some of the data from my csv file around the point of interest (The third column is of no interest):

9.991250000000E+04  -8.399371E+01   0.000000E+00
9.992500000000E+04  -8.108232E+01   0.000000E+00
9.993750000000E+04  -7.181630E+01   0.000000E+00
9.995000000000E+04  -7.190387E+01   0.000000E+00
9.996250000000E+04  -7.961070E+01   0.000000E+00
9.997500000000E+04  -8.090104E+01   0.000000E+00
9.998750000000E+04  -1.479405E+01   0.000000E+00
1.000000000000E+05  3.740311E+00    0.000000E+00
1.000125000000E+05  -6.665535E-01   0.000000E+00
1.000250000000E+05  -7.868803E+01   0.000000E+00
1.000375000000E+05  -8.149953E+01   0.000000E+00
1.000500000000E+05  -7.948487E+01   0.000000E+00
1.000625000000E+05  -7.436191E+01   0.000000E+00
1.000750000000E+05  -8.068216E+01   0.000000E+00
1.000875000000E+05  -7.998886E+01   0.000000E+00
1.001000000000E+05  -8.316663E+01   0.000000E+00

Here is how I am extracting the data

Frequency = data[:,0]
Intensity = data[:,1]

title("Frequency Intensity")
xlabel("Frequency [Hz]")
ylabel("Intensity [dBm]")
plot(Frequency, Intensity)
grid();

Edit: I would like my plot to look something like this where x shows the frequency and y shows the intensity in dBm. I simply want the marker I place to show the x,y coordinates on the plot.

FFT plot desired marker

Upvotes: 1

Views: 2818

Answers (2)

piRSquared
piRSquared

Reputation: 294258

Create a pd.Series from data

s = pd.DataFrame({
        'Frequency [Hz]': data[:, 0],
        'Intensity [dBm]': data[:, 1]
    }).set_index('Frequency [Hz]')['Intensity [dBm]']

Then plot with annotate

ax = s.plot(title='Frequency Intensity')
ax.set_ylabel(s.name)
point = (s.index[7], s.values[7])
ax.annotate('Marker', xy=point, xytext=(0.1, 0.95), textcoords='axes fraction',
            arrowprops=dict(facecolor='black', shrink=0.05),
            )

enter image description here

Upvotes: 1

Ilya V. Schurov
Ilya V. Schurov

Reputation: 8047

You probably want something like this:

import numpy as np
import matplotlib.pyplot as plt

# assuming we are in Jupyter
%matplotlib inline

frequency = np.array(
    [  99912.5,   99925. ,   99937.5,   99950. ,   99962.5,   99975. ,
       99987.5,  100000. ,  100012.5,  100025. ,  100037.5,  100050. ,
       100062.5,  100075. ,  100087.5,  100100. ])
intensity = np.array(
    [-83.99371  , -81.08232  , -71.8163   , -71.90387  , -79.6107   ,
     -80.90104  , -14.79405  ,   3.740311 ,  -0.6665535, -78.68803  ,
     -81.49953  , -79.48487  , -74.36191  , -80.68216  , -79.98886  ,
     -83.16663  ])

plt.title("Frequency Intensity")
plt.xlabel("Frequency [Hz]")
plt.ylabel("Intensity [dBm]")
plt.plot(frequency, intensity)
x = frequency[7]
y = intensity[7]
plt.plot([x], [y], 'v', color='red', ms=10)
plt.text(x, y, "({:0.2f}, {:0.2f})".format(x, y))
plt.grid()

picture with marker

Upvotes: 0

Related Questions