matplotlib自动确定tick的范围

matplotlib画图中tick的最大值往往很有可能小于数据中最大值,自编代码生成tick的范围,能够使得tick大于数据的最大值。,最后可以用    ax.set_xticks(rangelist)调用

    def ytickcalculation(ymax,n_points):
    #ymax 是数据中的最大值,npoints是tick 的个数
    	timer=0
    	while ymax<10:
    		timer=timer+1
    		ymax=ymax*10
    	while ymax>100:
    		ymax=ymax/10
    		timer=timer-1
    	increase=ymax/n_points
    	if increase<1:
    		increase=1
    	elif increase<2:
    		increase=2
    	elif increase<5:
    		increase=5
    	elif increase<10:
    		increase=10
    	elif increase<20:
    		increase=20
    	else:
    		increase=30
    	rangemax=increase*(int(ymax/increase)+2)# +1是让rangemax大于ymax,再+1是防止range时候最大值抹去
    	rangelist=np.array(range(0,rangemax,increase))/10**(timer)
    	return rangelist

猜你喜欢

转载自blog.csdn.net/qq_36145163/article/details/84668511