matplotlib中的三维图

时间:2014-06-16 19:15:26

标签: python graph matplotlib

我有三个相同大小的列表x,y和z,我想在图表上绘制。这是我的代码:

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(x, y, z, label = 'curve')
plt.show()

但是,我收到以下错误:

Traceback (most recent call last):
File "Graph.py", line 36, in <module>
ax.plot(x, y, z, label = 'curve')
      File "/usr/lib/python3/dist-packages/mpl_toolkits/mplot3d/axes3d.py", line 1513, in plot
    lines = Axes.plot(self, xs, ys, *args[argsi:], **kwargs)
      File "/usr/lib/python3/dist-packages/matplotlib/axes.py", line 4139, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/usr/lib/python3/dist-packages/matplotlib/axes.py", line 319, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "/usr/lib/python3/dist-packages/matplotlib/axes.py", line 281, in _plot_args
    raise ValueError('third arg must be a format string')
ValueError: third arg must be a format string

1 个答案:

答案 0 :(得分:2)

您似乎没有正确初始化3D轴。拳头,请确保您已正确导入所有内容:

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

然后,尝试在图中添加一个子图,而不是访问当前轴:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

如果没有解决问题,您需要提供更多详细信息。