交互式python matplotlib

时间:2015-06-05 13:51:45

标签: python matplotlib interactive

python noob here。我试图重新创建这个例子

from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt, mpld3
from matplotlib import colors 
from matplotlib.colors import colorConverter
import matplotlib.animation as animation
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

cc = lambda arg: colorConverter.to_rgba(arg, alpha=0.6)

xs = np.arange(0, 10, 0.4)
verts = []
zs = [0.0, 1.0, 2.0, 3.0]
for z in zs:
    ys = np.random.rand(len(xs))
    ys[0], ys[-1] = 0, 0
    verts.append(list(zip(xs, ys)))

poly = PolyCollection(verts, facecolors = [cc('r'), cc('g'), cc('b'),
                                           cc('y')])
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=zs, zdir='y')

ax.set_xlabel('X')
ax.set_xlim3d(0, 10)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, 4)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 1)
mpld3.show()

我想保存交互式情节(并将其发送给不使用python的人),所以mpld3.show()似乎可以做到这一点。只有我不断收到此错误

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/mpld3/_display.py", line 338, in show
    html = fig_to_html(fig, **kwargs)
  File "/Library/Python/2.7/site-packages/mpld3/_display.py", line 236, in fig_to_html
    figure_json=json.dumps(figure_json),
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: array([  0.,  10.]) is not JSON serializable

我明白这是因为某些东西是一个np数组而不是列表,但当我

type(verts) 

我得到了

<type 'list'>

所以我不确定那个&#34;数组([0,10。])&#34;是/如何解决这个问题。请提供有关如何提取/操作

类型对象的说明
<class 'mpl_toolkits.mplot3d.art3d.Poly3DCollection'>

请原谅我的高傲。谢谢你们所有人。

1 个答案:

答案 0 :(得分:4)

根据mpld3 github,&#34; mpld3&#34;目前不支持3D绘图,因此会产生您看到的错误

我遇到了你的问题,因为我正在寻找相同的解决方案(能够与没有Python的人分享交互式3D matplotlib情节)。我意识到这不是一个答案,但我认为它仍然值得分享? (我也是一个菜鸟,所以我甚至不能评论你的问题)