Axes3D.text()注释3D散点图

时间:2019-05-24 12:56:00

标签: python loops matplotlib text 3d

无法使3D文本注释散点图。

尝试了Axes3D.text,plt.text,但不断出现“缺少必需的位置参数s”的情况。您如何在3D中循环注释?

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import pandas as pd
import numpy as np

df = pd.read_csv (r'J:\Temp\Michael\Python\9785.csv')

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

#Scatter plot
for i in df.index:
    x = df.at[i,'x']
    y = df.at[i,'y']
    z = df.at[i,'h']
    ax.scatter(xs=x, ys=y, zs=z, s=20,color='red',marker='^')
    label = df.at[i,'to']
    Axes3D.text(x+0.8,y+0.8,z+0.8, label, zdir=x)

TypeError:text()缺少1个必需的位置参数:'s'

1 个答案:

答案 0 :(得分:0)

更改:ax = fig.add_subplot(111,projection ='3d')

至:ax = fig.gca(projection ='3d')

解决了问题。用过的ax.text。

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import pandas as pd
import numpy as np

df = pd.read_csv (r'J:\Temp\Michael\Python\9785.csv')

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

#Scatter plot
for i in df.index:
    df.set_index('to')
    x = df.at[i,'x']
    y = df.at[i,'y']
    z = df.at[i,'h']
    ax.scatter(xs=x, ys=y, zs=z, s=20,color='red',marker='^')
    ax.text(x+0.8,y+0.8,z+0.8, df.at[i,'to'], size=10, zorder=1)