如何在python matplotlib pyplot中设置xlim?

时间:2013-12-02 15:07:18

标签: python matplotlib

我有这段代码:

import matplotlib.pyplot as pl
pl.plot(np.log(alphas_lasso),coefs_lasso.T) #this generate a plot which xlim=[-8,-2]
for alpha in alpha_min:
    pl.axvline(np.log(alpha),0,1) #add vertical line for every value of alpha
pl.xlim([-10,0]) # the xlim value I would like to set
pl.xlabel('Log(alpha)')
pl.ylabel('coefficients')
pl.title('Lasso Path')
pl.axis('tight')
pl.show()
print "log(alpha_min)=",log(alpha_min)

它有效,但x轴从-8到-2,而我想从-10到0,错误在哪里?

1 个答案:

答案 0 :(得分:2)

您使用pl.axis('tight')将xrange设置为默认值。尝试删除该行,或将其放在xlim命令之前。

pl.axis('tight')对{{1}}说: “更改x和y轴限制,以便显示所有数据。”