0

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.

2
  • Can you display the actual error ? What's your version of matplotlib ? Commented Aug 22, 2012 at 8:39
  • @PierreGM: I just added the error message above. The version of matplotlib is 1.1.0. Thanks Commented Aug 23, 2012 at 4:33

1 Answer 1

3

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])

Sign up to request clarification or add additional context in comments.

1 Comment

Note that the arrays must be one dimensional (and matrices are always 2D). So if you want to plot matrices directly, you would have to plot X.A.ravel(), Y.A.ravel() (X.A is just an array view of the matrix).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.