mintaka
mintaka

Reputation: 1012

How can I use matrix from numpy as the input of some plotting functions in matplotlib?

I want to plot four points (or lines connected to each other) in 3D coordinates. And the coordinates are stored in X, Y and Z. For example, I have the following lines:

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.matrix('-1, 1, 1, 2')
Y = np.matrix('1, 2, 4, 6')
Z = np.matrix('3, 4, 2, 1')

ax.plot(X, Y, Z)
plt.show()

But after running, there will be a type error. I guess it's due to the input of the matrix type in numpy. Anybody know how to easily solve this problem?

Error message:

Traceback (most recent call last):
  File "C:\Users\I077165\Desktop\tmp.py", line 11, in <module>
    ax.plot(X, Y, Z)
  File "C:\Python27\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1312, in plot
    lines = Axes.plot(self, xs, ys, *args[argsi:], **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 3848, in plot
    for line in self._get_lines(*args, **kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 323, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 284, in _plot_args
    raise ValueError, 'third arg must be a format string'
ValueError: third arg must be a format string

Thanks.

Upvotes: 0

Views: 1615

Answers (1)

Roland Smith
Roland Smith

Reputation: 43505

Use np.array() instead of np.matrix() for all three datasets.

So for the X data, np.matrix('-1, 1, 1, 2') should become np.array([-1, 1, 1, 2])

Upvotes: 3

Related Questions