Cam*_*ust 53 macos plot matplotlib ipython jupyter-notebook
(Mac OSX 10.10.5)
我可以从matplotlib网站复制http://matplotlib.org/gallery.html#mplot3d的3D散点图示例代码http://matplotlib.org/examples/mplot3d/scatter3d_demo.html,但情节呈现为静态图像.我无法点击图表并动态旋转以查看3D绘图数据.
我使用示例代码实现了静态3D绘图 - 使用(a)终端内的ipython,(b)终端内的ipython笔记本,以及(c)从Anaconda启动器启动的ipython笔记本.
我想我错过了一些非常基本的步骤作为假设的知识.
在过去的学习中,plotting已经打开了一个GUI Python App,它有一个图形查看器.(下面显示的代码中的解决方案2打开了这个.)也许我需要知道将输出图导出到该显示方法的代码?(是的,使用%matplotlib(仅)作为没有内联或笔记本的第一行,如下面代码块中的注释所示.)
作为ipython笔记本中的一个例子:
# These lines are comments
# Initial setup from an online python notebook tutorial is below.
# Note the first line "%matplotlib inline" this is how the tutorial has it.
# Two solutions 1. use: "%matplotlib notebook" graphs appear dynamic in the notebook.
# 2. use: "%matplotlib" (only) graphs appear dynamic in separate window.
# ( 2. is the best solution for detailed graphs/plots. )
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
pd.set_option('html',False)
pd.set_option('max_columns',30)
pd.set_option('max_rows',10)
# What follows is a copy of the 3D plot example code.
# Data is randomly generated so there is no external data import.
def randrange(n, vmin, vmax):
return (vmax-vmin)*np.random.rand(n) + vmin
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -60, -25), ('b', '^', -30, -5)]:
xs = randrange(n, 23, 50)
ys = randrange(n, 0, 100)
zs = randrange(n, zl, zh)
ax.scatter(xs, ys, zs, c=c, marker=m)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
Run Code Online (Sandbox Code Playgroud)
有人可以识别我错过的东西吗?
查看Python 3.3.6文档,第25.1节可能是tkinter包...
tkinter包("Tk接口")是Tk GUI工具包的标准Python接口.Tk和tkinter都可以在大多数Unix平台以及Windows系统上使用.
我认为,这与GUI程序的开发有关,所以我不确定这是否相关.(正确,这不是解决方案所必需的.)
jak*_*vdp 123
使用%matplotlib notebook而不是%matplotlib inline在IPython笔记本中获取嵌入式交互式数字 - 这需要最新版本的matplotlib(1.4+)和IPython(3.0+).
对于 Colab 环境,我发现 HTML() 函数最有用:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from numpy.random import rand
from IPython.display import HTML
from matplotlib import animation
m = rand(3,3) # m is an array of (x,y,z) coordinate triplets
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for i in range(len(m)): # plot each point + it's index as text above
x = m[i,0]
y = m[i,1]
z = m[i,2]
label = i
ax.scatter(x, y, z, color='b')
ax.text(x, y, z, '%s' % (label), size=20, zorder=1, color='k')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
def animate(frame):
ax.view_init(30, frame/4)
plt.pause(.001)
return fig
anim = animation.FuncAnimation(fig, animate, frames=200, interval=50)
HTML(anim.to_html5_video())
Run Code Online (Sandbox Code Playgroud)