2017-01-30 24 views
1

我使用matplotlib在Julia中定義了以下動畫。動畫將可見區域沿着藍線從左向右移動。我沿着藍線放置了幾個文本元素。我希望一旦它們出現在視野內(如軸限制所指定的那樣)就可以使它們可見。但是,正如您所看到的,文本元素在移動到實際座標軸之前在軸外部的白色區域上可見。我怎樣才能防止這一點?在Julia/matplotlib動畫過程中保留文本內的軸

using PyPlot, PyCall 
@pyimport matplotlib.animation as animation 


# define initial plot 
fig = figure(facecolor="white") 
ax = gca() 
ax[:set_ylim](-1,+1) 


plot([-10,10],[0,0],"b-*",linewidth=2.0) 
for i = -10:2:10 
    text(i,0.3,"$(i)",fontsize=30) 
end 


xdata = -10:0.1:10 
# use animate function to change xlim 
function animate(k) 
    k+=1 
    # change axis limits 
    lim = ax[:set_xlim](xdata[k]-1,xdata[k]+1) 
    return (lim,nothing) 
end 
numMaxFrames = length(xdata) 
anim = animation.FuncAnimation(fig, animate, frames=numMaxFrames, interval=50,repeat=true) 

:10是軸外 enter image description here

+0

的'10'是那裏,因爲你編程它在那裏:'文本(10,0.3,「10」)'把文本放在位置(10,0.3)。從你的問題來看,你並不清楚你想要什麼。所以請相應地編輯它,清楚地說明預期的行爲。 – ImportanceOfBeingErnest

+0

@ImportanceOfBeingErnest我編輯了這個問題。我希望這更容易理解。 – miga89

+0

因此,在上面的情節中,你想讓'10'完全不可見,或者將它放在不同的位置? – ImportanceOfBeingErnest

回答

1

我通過設置選項clip_on定義文本元素時解決了這個問題:

text(i,0.3,"$(i)",fontsize=30,clip_on=true)