严重对齐的3D标签 - Matplotlib

时间:2016-03-14 15:55:44

标签: python animation matplotlib plot 3d

我使用matplotlib创建散点图的3D动画,我发现很难让刻度标签与刻度线对齐。

A screen grab from the plot

您可以看到刻度标签,特别是y轴刻度,看起来不像刻度线。我在这里看到了解决方案:tick label positions for matplotlib 3D plot但它对我不起作用,因为我不想手动设置标签,因为我使用FuncFormatter格式化我的刻度标签(所以我不能使用ax.set_ylabels(...))。如何在使用funcformatter时对齐它们?相关的代码部分:

def kilometer(x,pos):
label=x/1000
return label

formatter=mtick.FuncFormatter(kilometer)

# Data sourcing function
def getData(reader):
    x=[]
    y=[]
    z=[]
    j=0
    for row in reader:
        t=float(row[0])
        x.append(float(row[1]))
        y.append(float(row[2]))
        z.append(float(row[3]))
        j+=1
        if j==particleNo: break
    return t,x,y,z

# animation function.  This is called sequentially
def animate(i, fig, reader):
    #fig.subplots_adjust(bottom=1.0)
    if i<rowsToPlot:
        ax.cla()
        x=[]
        y=[]
        z=[]
        t,x,y,z=getData(reader)
        timeTitle=ax.text2D(0.2, 0.9, "Time = %.2e s" % t,      horizontalalignment='center', verticalalignment='top',transform=ax.transAxes,fontsize=15)
        scat=ax.scatter(x,y,z,s=5)
        ax.scatter(x, y, s=2, alpha=0.05, c='r', zs=zmin, zdir='z')
        ax.scatter(y, z, s=2, alpha=0.05, c='r', zs=xmin, zdir='x')
        ax.scatter(x, z, s=2, alpha=0.05, c='r', zs=ymax, zdir='y')
        ax.set_xlim(xmin, xmax)
        ax.set_ylim(ymin, ymax)
        ax.set_zlim(zmin, zmax)
        ax.set_xlabel('\n$x$ [km]\n')
        ax.set_ylabel('\n$y$ [km]\n')
        ax.set_zlabel('\n$z$ [km]\n')
        ax.xaxis.set_major_formatter(formatter)
        ax.yaxis.set_major_formatter(formatter)
        ax.zaxis.set_major_formatter(formatter)
        ax.tick_params(axis='y', which='major', rotation=-15, verticalalignment='baseline',horizontalalignment='left')
        return timeTitle, scat,

0 个答案:

没有答案