2014-05-08 150 views
1

最後一個xtick可能是數據的最後一個值怎麼可能?熊貓和matplotlib xtick最大值設置

import pandas as pd 
import matplotlib.pyplot as plt 
a = [583, 1379, 1404, 5442, 5512, 5976, 5992, 6111, 6239, 6375, 6793, 6994, 7109, 7149, 7210, 7225, 7459, 8574, 9154, 9417, 9894, 10119, 10355, 10527, 11933, 12170, 12172, 12273, 12870, 13215, 13317, 13618, 13632, 13790, 14260, 14282, 14726, 15156, 19262, 21501, 21544, 21547, 21818, 21939, 22386, 22622, 22830, 23898, 25796, 26126, 26340, 28585, 28645, 28797, 28808, 28878, 29167, 29168, 31161, 31225, 32284, 32332, 32641, 33227, 34175, 34349, 34675, 34772, 34935, 35086] 

d = pd.DataFrame(a) 
d.hist(bins=50) 
f = plt.figure() 
plt.xlabel("xTEST") 
plt.ylabel("yTEST") 
plt.subplots_adjust(bottom=.25, left=.25) 

plt.ticklabel_format(style = 'plain') 

plt.title('Title here!', color='black') 
plt.xlim(xmin=1, xmax=35086) 
plt.show() 
+0

怎麼可能將最後一個xtick設置爲35086而不是35000? – user977828

+0

相關問題:[如何在固定位置設置刻度,matplotlib](http://stackoverflow.com/q/17129947/832621) –

回答

1

如果您想使用由pandas創建的相同圖形,則無法創建新圖形(請參見下文)。可以設置刻度位置plt.xlim()

plt.xticks(seq) 

後手動添加plt.xticks()其中序列seq可以pd.np.linspace(1, 35086, 5)例如,或手動給定,如:

seq = [1, 10000, 20000, 35086] 

最後的代碼將是:

import pandas as pd 
import matplotlib.pyplot as plt 
a = [583, 1379, 1404, 5442, 5512, 5976, 5992, 6111, 6239, 6375, 6793, 6994, 7109, 7149, 7210, 7225, 7459, 8574, 9154, 9417, 9894, 10119, 10355, 10527, 11933, 12170, 12172, 12273, 12870, 13215, 13317, 13618, 13632, 13790, 14260, 14282, 14726, 15156, 19262, 21501, 21544, 21547, 21818, 21939, 22386, 22622, 22830, 23898, 25796, 26126, 26340, 28585, 28645, 28797, 28808, 28878, 29167, 29168, 31161, 31225, 32284, 32332, 32641, 33227, 34175, 34349, 34675, 34772, 34935, 35086] 

d = pd.DataFrame(a) 
d.hist(bins=50) 
ax = plt.gca() 
ax.set_xlabel("xTEST") 
ax.set_ylabel("yTEST") 
plt.subplots_adjust(bottom=.25, left=.25) 
plt.ticklabel_format(style = 'plain') 

ax.set_title('Title here!', color='black') 
ax.set_xlim(xmin=1, xmax=35086) 
ax.set_xticks(pd.np.linspace(1, 35086, 5)) 
plt.show() 

給予:

enter image description here

1

不知道你需要什麼,但你需要刪除的行

f = plt.figure() 

如果你想你的d直方圖和標題/標籤/ XLIM等是在相同的圖表。

+0

謝謝,但現在最後的xtick是35000而不是35086.是否是一種方法將它設置爲35086? – user977828