matplotlib 3d图与更改标签

时间:2013-05-09 18:25:16

标签: python graph 3d matplotlib axis

所以我有一个3D实时更新图!它一次只显示一个点,所以我可以轻松跟踪点的运动!但问题出在这里:

无论我做什么,该点始终位于图形的中心,并且轴上的刻度标记会发生变化。这使我的生活变得非常困难,因为我没有看到关于这一点的动议。这是我的代码:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from pylab import *
import time
import pandas as pd
import pickle
def pickleLoad(pickleFile):
    pkl_file = open(pickleFile, 'rb')
    data = pickle.load(pkl_file)
    pkl_file.close()
    return data
data = pickleLoad('/Users/ryansaxe/Desktop/kaggle_parkinsons/accelerometry/LILY_dataframe')
data = data.reset_index(drop=True)
df = data.ix[0:,['x.mean','y.mean','z.mean','time']]
ion()
fig = figure()
ax = fig.add_subplot(111, projection='3d')
count = 0
plotting = True
labels = range(-10,11)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
ax.set_yticklabels(labels)
ax.set_xticklabels(labels)
ax.set_zticklabels(labels)
lin = None
while plotting:
    df2 = df.ix[count]
    count += 1
    xs = df2['x.mean']
    ys = df2['y.mean']
    zs = df2['z.mean']
    t = df2['time']
    ax.set_title(t)
    if lin is not None:
        lin.remove()
    lin = ax.scatter(xs, ys, zs)

    draw()
    pause(0.01)
    if count > 100:
        plotting = False
ioff()
show()

这是一个数据示例:

     x.mean    y.mean    z.mean                 time
0 -1.982905  3.395062  8.558263  2012-01-18 14:00:03
1  0.025276 -0.399172  7.404849  2012-01-18 14:00:04
2 -0.156906 -8.875595  1.925565  2012-01-18 14:00:05
3  2.643088 -8.307801  2.382624  2012-01-18 14:00:06
4  3.562265 -7.875230  2.312898  2012-01-18 14:00:07
5  4.441432 -7.907592  2.851774  2012-01-18 14:00:08
6  4.124187 -7.854146  2.727229  2012-01-18 14:00:09
7  4.199698 -8.135596  2.677706  2012-01-18 14:00:10
8  4.407856 -8.133449  2.214902  2012-01-18 14:00:11
9  4.096238 -8.453822  1.359692  2012-01-18 14:00:12

那么我该怎样做才能使刻度线固定,这样点移动而不是刻度线变化?

3 个答案:

答案 0 :(得分:3)

Axes3D对象(您的ax变量)具有以下方法:set_xlimset_ylimset_zlim。您可以使用它们来修复轴的极限。

文档:

修改

使用set_xlim等对我有用。这是我的代码:

#!python2

from mpl_toolkits.mplot3d import Axes3D
from pylab import *

data = [
    [-1.982905,  3.395062,  8.558263,  '2012-01-18 14:00:03'],
    [ 0.025276, -0.399172,  7.404849,  '2012-01-18 14:00:04'],
    [-0.156906, -8.875595,  1.925565,  '2012-01-18 14:00:05'],
    [ 2.643088, -8.307801,  2.382624,  '2012-01-18 14:00:06'],
    [3.562265, -7.875230,  2.312898,  '2012-01-18 14:00:07'],
    [4.441432, -7.907592,  2.851774,  '2012-01-18 14:00:08'],
    [4.124187, -7.854146,  2.727229,  '2012-01-18 14:00:09'],
    [4.199698, -8.135596,  2.677706,  '2012-01-18 14:00:10'],
    [4.407856, -8.133449,  2.214902,  '2012-01-18 14:00:11'],
    [4.096238, -8.453822,  1.359692,  '2012-01-18 14:00:12'],
]

ion()
fig = figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
ax.set_xlim((-10, 11))
ax.set_ylim((-10, 11))
ax.set_zlim((-10, 11))

lin = None
for x, y, z, t in data:
    ax.set_title(t)
    if lin is not None:
        lin.remove()
    lin = ax.scatter(x, y, z)
    draw()
    pause(0.1)

ioff()
show()

修改2

您可以查看关闭默认启用的轴的自动缩放功能。也许这会覆盖set_lim方法。

文档:

答案 1 :(得分:1)

我遇到了与你完全相同的问题。我发现一种让它工作的方法是每次你想要添加一个新点时重置轴限制,所以你的循环看起来像:

while plotting:
    df2 = df.ix[count]
    count += 1
    xs = df2['x.mean']
    ys = df2['y.mean']
    zs = df2['z.mean']
    t = df2['time']
    ax.set_title(t)
    if lin is not None:
        lin.remove()
    lin = ax.scatter(xs, ys, zs)
    ax.set_xlim((-10, 11))
    ax.set_ylim((-10, 11))
    ax.set_zlim((-10, 11))

    draw()
    pause(0.01)
    if count > 100:
        plotting = False

我有一种感觉,每次重置限制是解决这个问题的一种不好的方法,所以我问question有关它,并希望能找到答案。如果你想要的只是看到一些有用的东西,那么希望循环内的极限变化可以解决问题

答案 2 :(得分:0)

您可能希望保持轴范围不变。例如,这可以通过axis()来完成。

另见这些答案:
How to set axis range
y-axis limit
enforce axis range