如何自动设置x轴的大小

时间:2016-06-20 17:40:51

标签: python python-3.x matplotlib

变量x从0变为19.但我有其他例子,它将从0到28或从0到5.所以,我怎样才能将其设置为自动而不是像我所做的那样写所有数字在“plt.xticks”?

    for i in range(x.shape[1]): 
        xA=x[:, i].reshape(-1,1)
        skf = StratifiedKFold(y, n_folds=2, shuffle=True)
        scoresSKF = cross_validation.cross_val_score(clf, xA, y, cv=skf)

        plt.ylabel('Accuracy')
        plt.xlabel('Features')    
        plt.title('Accuracy by feature: Random Forest')

        plt.xticks([0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19], rotation = 'vertical')
    plt.show()

2 个答案:

答案 0 :(得分:1)

你必须使用来自numpy http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html

arange
import numpy
...
x_ticks = numpy.arange(20) # generate a seq from 0 to 19, step is one.
plt.xticks(x_ticks, rotation = 'vertical')
...

答案 1 :(得分:1)

import numpy
...
x_ticks = numpy.arange(i+1) #As the loop starts in 1, the plot will be 0->18. So I add 1 because I want the plot 0->19
plt.xticks(x_ticks, rotation = 'vertical')