2017-04-25 25 views
1

我試圖從y軸上的整個範圍(10)顯示我的圖形,但是最低的y值是4,最高的是7.我如何讓它顯示範圍內所有點的y軸,而不僅僅是哪些地方有價值?如何防止matplotlib.pyplot僅顯示數據存在的圖形部分?

我的腳本是:

import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 
import datetime as dt 

#X axis data 
dates = ['03/04/2017', '04/04/2017', '05/04/2017', '06/04/2017', '07/04/2017', '08/04/2017', '09/04/2017', 
     '10/04/2017', '11/04/2017', '12/04/2017', '13/04/2017', '14/04/2017', '15/04/2017', '16/04/2017'] 

#Y axis data 
occurence_number = [7,5,4,6,7,6,6,4,5,7,7,6,7,7] 

#formatting the date information 
x = [dt.datetime.strptime(d, '%d/%m/%Y').date() for d in dates] 
y = occurence_number 

#labeling the x and y axis 
plt.xlabel('Date') 
plt.ylabel('Instances of Behavior') 

#here I tried to set the yticks in an attempt to show them all 
plt.yticks(range(10)) 

#formating y axis values for matplotlib 
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y')) 
plt.gca().xaxis.set_major_locator(mdates.DayLocator()) 

plt.plot(x,y) 
plt.gcf().autofmt_xdate() 
plt.show() 

我曾嘗試使用:

plt.set_autocale_on(False) 

會導致一個錯誤:

"'module' object has no attribute 'set_autocale_on'" 

其他的事情,我發現我不瞭解如何實施。該圖目前顯示正確的值,但我想要「縮小」,以便它仍然顯示0 - > 4和7 - > 10的y標籤,其中沒有值。

+0

'plt.gca()。set_ylim(0,10)'或'plt.ylim(0,10)' – Paul

+0

這個偉大的工程! –

回答

0

我能夠使用@Paul提供的答案解決問題。

plt.gca().set_ylim(0,10)