2013-07-10 127 views
2

我一直在使用主機來繪製我的圖表,我想從我的x軸上取下十進制數字。我的代碼如下簡單,代表我需要的東西。我希望x範圍與RUNS矢量完全相同。非常感謝。如何設置只有整數的x軸刻度標記?

import matplotlib.pyplot as plt 
from mpl_toolkits.axes_grid1 import host_subplot 
import numpy as np 
import mpl_toolkits.axisartist as AA 
import matplotlib.gridspec as gridspec 

gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1]) 

RUNS = [1,2,3,4,5] 
NV = [26.3, 28.4, 28.5, 28.45, 28.5] 

host = host_subplot(gs[0], axes_class = AA.Axes) 
host.set_xlabel("Iteration") 
host.set_ylabel("Stress (MPa)") 
sv, = host.plot(RUNS,NV, marker = 'o', color = 'gray') 

plt.grid(True) 
plt.show() 

回答

5

您可以明確地設置刻度標記標籤顯示您的情節之前:

plt.xticks(RUNS) 
5

您可以使用MaxNLocator主要刻度標記設置爲整數爲好,但其不平靜的那樣簡單。

import matplotlib.pyplot as plt 
from mpl_toolkits.axes_grid1 import host_subplot 
import numpy as np 
import mpl_toolkits.axisartist as AA 
import matplotlib.gridspec as gridspec 
from matplotlib.ticker import MaxNLocator ## Import MaxNLocator 

gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1]) 

RUNS = [1,2,3,4,5] 
NV = [26.3, 28.4, 28.5, 28.45, 28.5] 



host = host_subplot(gs[0], axes_class = AA.Axes) 
host.set_xlabel("Iteration") 
host.set_ylabel("Stress (MPa)") 

x_ax = host.axes.get_xaxis() ## Get X axis 
x_ax.set_major_locator(MaxNLocator(integer=True)) ## Set major locators to integer values 

sv, = host.plot(RUNS,NV, marker = 'o', color = 'gray') 

plt.grid(True) 
plt.show() 
+0

謝謝你,這也是一個很好的解決方案! – user2501498

+0

這比使用'xticks'更好的解決方案。 – tacaswell