【发布时间】:2016-01-30 21:25:35
【问题描述】:
(Mac OSX 10.10.5)
我可以从 matplotlib 网站 mplot3d 复制 3D 散点图 scatter3d_demo.py 的示例代码,但是该图呈现为静态图像。我无法单击图形并动态旋转以查看 3D 绘制数据。
我已经使用示例代码实现了静态 3D 绘图 - 使用 (a) 终端内的 ipython,(b) 终端内的 ipython 笔记本,以及 (c) 从 Anaconda 启动器启动的 ipython 笔记本。
我认为我缺少一些非常基本的步骤作为假设的知识。
在过去的学习中,绘图打开了一个带有图形查看器的 GUI Python App。 (下面显示的代码中的解决方案 2 会打开它。)也许我需要知道将输出图导出到该显示方法的代码? (是的,使用 %matplotlib(仅)作为第一行,没有内联或笔记本,如下面代码块中的 cmets 所示。)
以 ipython notebook 为例:
# 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()
有人能找出我缺少的东西吗?
查看 Python 3.3.6 文档,第 25.1 节可能是 tkinter 包 ...
tkinter 包(“Tk 接口”)是 Tk GUI 工具包的标准 Python 接口。 Tk 和 tkinter 在大多数 Unix 平台以及 Windows 系统上都可用。
不过,我认为这与 GUI 程序的开发有关,所以我不确定这是否相关。 (正确,解决方案不需要。)
【问题讨论】:
-
尝试添加对
ax.mouse_init()的呼叫 -
OK,在 plt.show() 之前在哪里?
-
我不确定这是否重要,但这就是我要说的地方。
-
好的,谢谢。我在这里试过了,没有用。我刚刚在我原来的问题中添加了一个可能的解决方案的新方向。
-
@Matt Nice tidy 编辑了这个问题。谢谢。我想这是我加入 SO 的第一篇文章。
标签: macos matplotlib plot jupyter-notebook ipython