-1
x = [[(1+2j) ,(2+8j), (4+1j), (6+8j), (7+3j), (8+2j)],
     [(3+8j), (5+1j), (7+5j), (3+2j), (6+1j), (3+1j)],
     [(1+5j), (5+4j), (2+9j), (9+5j), (8+1j), (4+1j)]]

I have a complex numpy array and I want to plot it with matplotlib.

t = np.linspace(0, 2700, 2700)
plt.plot(t,x[0])
plt.xlabel('Range')
plt.ylabel('I+jQ')
plt.xlim([0, 2700])
plt.show()

When I run this code I get a failure like this:

/home/user/.local/lib/python3.8/site-packages/numpy/core/_asarray.py:102: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)

I mean when I run this code I get a plot but this code draw only real part.

gboffi
  • 22,939
  • 8
  • 54
  • 85
  • You could plot `t, Re(number) and Im(number)` in three dimensions? – warped May 08 '21 at 18:38
  • No, I want to plot them in 2 dimensions. X axis will be time and y axis will be a+jb and a+jb will came from only x[0]. – python_student May 08 '21 at 18:47
  • FYI, the way to write numbers with real & imaginary parts is `1+2j`, not `1+j2`. – aneroid May 08 '21 at 19:13
  • Each `y` value has 2 numbers, the `a` and `b`. We don't know what you to show in 2d. – hpaulj May 08 '21 at 19:32
  • I've recently posted [an answer](https://stackoverflow.com/a/67654184/2749397) according to my (limited) understanding of your question. Could you please provide some feedback, so that I can delete my answer if I misunderstood your request? – gboffi May 26 '21 at 14:43

2 Answers2

0

Matplotlib doesn't handle complex values. Indeed, how would it even do that? What would it look like? If you want to see real vs imaginary, then extract the real parts to x, extract the imaginary parts to y, and plt.plot(x,y).

https://www.geeksforgeeks.org/how-to-plot-a-complex-number-in-python-using-matplotlib/

Since you're trying to plot these against time, maybe you want a 3D scatter plot or 3D line plot. You can do that, again by separating the reals and imaginaries into separate arrays, and plotting that against time.

Update

One useful way to plot quadrature data is to plot I vs Q as a line plot. Consider this example:

import numpy as np
import matplotlib.pyplot as plt

I = np.sin( np.arange(200) * 6.2 / 200.0 I
Q = np.cos( np.arange(200) * 6.2 / 200.0 )
I += np.random.uniform( 0.0, 0.1, 200 ) - 0.05
Q += np.random.uniform( 0.0, 0.1, 200 ) - 0.05

plt.plot( I, Q )
plt.show()

Which produces: enter image description here

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Actually I have some In-phase and Quadrature (I&Q) datas and I want to plot it in 2 dimensions. X axis will be I+jQ data and Y axis will be time from 0 to 2700. IS there any way to do. – python_student May 08 '21 at 18:49
  • No there is not. You can’t fully represent a complex number as a single real number. You are asking the mathematically impossible – Jody Klymak May 08 '21 at 19:36
  • 1
    One useful way to do is as a Lissajous display, basically a vector rotating around the circle. You can do that as I described in my answer, doing a line plot of I vs Q. – Tim Roberts May 08 '21 at 19:44
0

enter image description here

A suitable way to plot a complex function of real time is to plot the imaginary part versus the real part and make explicit the time dependency using a color bar.

The tricky part is to plot the Lissajous curve with a changing color, Matplotlib has no direct support for that but it can be done using a LineCollection (you can see, e.g., this answer of mine to delve into the gory details).

Here it is the code used to produce the figure at the top.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

# prepare some data
t, ω = np.linspace(0, 20, 1001), np.pi/2
c = np.exp(1j*ω*t)*(1+0.5*np.sin(3.1*ω*t))*np.exp(-0.05*t)

#prepare to plot
fig, ax = plt.subplots()
x, y = c.real, c.imag
points = np.array([x, y]).T.reshape(-1,1,2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(segments, cmap='plasma', linewidth=3)
lc.set_array(t)
ax.add_collection(lc) ; ax.autoscale()
cb = plt.colorbar(lc)
ax.set_xlabel('Re(c)')
ax.set_ylabel('Im(c)')
cb.set_label('Time/s')
plt.show()
gboffi
  • 22,939
  • 8
  • 54
  • 85