交互式 python matplotlib
Interactive python matplotlib
python 菜鸟在这里。我正在尝试重新创建此示例
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
我明白这是因为 something 是 np 数组而不是列表,但是当我
type(verts)
我明白了
<type 'list'>
所以我不确定 "array([ 0., 10.])" 是什么/如何解决这个问题。请说明如何提取/操作
类型的对象
<class 'mpl_toolkits.mplot3d.art3d.Poly3DCollection'>
请原谅我的笨拙。谢谢大家。
根据 mpld3 github,“mpld3 当前不支持 3D 图”,因此会产生您所看到的错误
我 运行 回答了你的问题,因为我正在寻找相同的解决方案(与没有 Python 的人共享交互式 3D matplotlib 绘图的能力)。我意识到这不是答案,但我认为它仍然值得分享? (我也是菜鸟,所以我什至不能评论你的问题)
python 菜鸟在这里。我正在尝试重新创建此示例
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
我明白这是因为 something 是 np 数组而不是列表,但是当我
type(verts)
我明白了
<type 'list'>
所以我不确定 "array([ 0., 10.])" 是什么/如何解决这个问题。请说明如何提取/操作
类型的对象<class 'mpl_toolkits.mplot3d.art3d.Poly3DCollection'>
请原谅我的笨拙。谢谢大家。
根据 mpld3 github,“mpld3 当前不支持 3D 图”,因此会产生您所看到的错误
我 运行 回答了你的问题,因为我正在寻找相同的解决方案(与没有 Python 的人共享交互式 3D matplotlib 绘图的能力)。我意识到这不是答案,但我认为它仍然值得分享? (我也是菜鸟,所以我什至不能评论你的问题)