Skip to content

phase_plane_plot generates warnings with latest version of numpy (2.5.x) #1252

Description

@h-bauer

Problem description

Here is a block of code generating an extensive list of warnings since i upgraded numpy from 2.4.6 to 2.5.x:

import control as ct
import numpy as np
import matplotlib.pyplot as plt

sys = ct.ss([[0, 1], [32.7, -0.556]], [[0], [55.6]], [[1, 0]], [[0]])
axis_limits = [-2 * np.pi, 2 * np.pi, -15, 15]
ct.phase_plane_plot(sys, axis_limits)
plt.show()

Text result:

/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/control/nlsys.py:1782: ComplexWarning: Casting complex values to real discards the imaginary part
  y[:, i] = sys._out(t, soln.y[:, i], u[:, i])
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/cbook.py:1811: ComplexWarning: Casting complex values to real discards the imaginary part
  return math.isfinite(val)
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/cbook.py:1408: ComplexWarning: Casting complex values to real discards the imaginary part
  return np.asanyarray(x, float)
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/transforms.py:1913: ComplexWarning: Casting complex values to real discards the imaginary part
  return affine_transform(values, mtx)
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/control/nlsys.py:1782: ComplexWarning: Casting complex values to real discards the imaginary part
  y[:, i] = sys._out(t, soln.y[:, i], u[:, i])
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/cbook.py:1811: ComplexWarning: Casting complex values to real discards the imaginary part
  return math.isfinite(val)
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/cbook.py:1408: ComplexWarning: Casting complex values to real discards the imaginary part
  return np.asanyarray(x, float)
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/transforms.py:1913: ComplexWarning: Casting complex values to real discards the imaginary part
  return affine_transform(values, mtx)
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/control/nlsys.py:1782: ComplexWarning: Casting complex values to real discards the imaginary part
  y[:, i] = sys._out(t, soln.y[:, i], u[:, i])
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/cbook.py:1811: ComplexWarning: Casting complex values to real discards the imaginary part
  return math.isfinite(val)
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/cbook.py:1408: ComplexWarning: Casting complex values to real discards the imaginary part
  return np.asanyarray(x, float)
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/transforms.py:1913: ComplexWarning: Casting complex values to real discards the imaginary part
  return affine_transform(values, mtx)
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/control/nlsys.py:1782: ComplexWarning: Casting complex values to real discards the imaginary part
  y[:, i] = sys._out(t, soln.y[:, i], u[:, i])
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/cbook.py:1811: ComplexWarning: Casting complex values to real discards the imaginary part
  return math.isfinite(val)
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/cbook.py:1408: ComplexWarning: Casting complex values to real discards the imaginary part
  return np.asanyarray(x, float)
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/transforms.py:1913: ComplexWarning: Casting complex values to real discards the imaginary part
  return affine_transform(values, mtx)
/home/myusername/.pyenv/versions/3.14.4/envs/myenv/lib/python3.14/site-packages/matplotlib/transforms.py:1913: ComplexWarning: Casting complex values to real discards the imaginary part
  return affine_transform(values, mtx)

It does not happen for every type of phase plots, but especially in the case of unstable equilibrium points. When it happens, it is always the same ComplexWarning. It does not prevent the plot to be produced though. The suppress_warnings optional parameter has no effect on this.

Here is an extract of my pip list (i did a complete pip upgrade prior to this report):

package                   Version
------------------------- -----------
control                   0.10.2
jupyterlab                4.6.4
matplotlib                3.11.2
numpy                     2.5.3
scipy                     1.18.1
slycot                    0.7.0
sympy                     1.14.0

Problem analysis

I managed to trace down the origin of the warning to two separate instructions by turning on warnings as error.

The first warning occurs at nlsys.py:1782:

y[:, i] = sys._out(t, soln.y[:, i], u[:, i])

The initial y is a np.zeros (defaulting to floats), and the slicing has to drop the imaginary part from the result of sys._out.

The second place where a warning occurs is at phaseplot:909:

out += ax.plot(traj[0], traj[1], color=color, linestyle=linestyle, zorder=zorder)

The plot function drops the imaginary part of traj[0] or traj[1].

Possible fix

I managed to get rid of the warnings by adding a .real call to the two following lines:

  • nlsys.py:1782 (fix the first warning):
y[:, i] = sys._out(t, soln.y[:, i], u[:, i]).real
  • phaseplot.py:1210 (fix all the other warnings by forcing the separatrice trajectories to have real coordinates):
return traj[:, inrange].real

I tested this two fixes in my virtualenv, and now I can use phase_plane_plot without warnings again.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      Sponsor
      SponsoredKunjungi sekarang
      Promo