2016-06-09 27 views
4

使用Matplotlib,我有两个子图,我希望它们具有相同的自定义字符串xticks。多个子图的自定义xticks?

下面是一个小例子,我试过到目前为止:

import matplotlib.pyplot as plt 
f, axs = plt.subplots(ncols=2, sharex=True) 
plt.xticks(range(6), [str(x)+"foo" for x in range(6)], rotation='45') 
for i in range(2): 
    ax = axs[i] 
    ax.plot(range(6), range(6)) 
f.show() 

产生这样的输出:

output of code example

注意左边的xticks是旋转。我怎样才能做到这一点?

如果我删除了sharex=True,则左侧子图没有自定义xticks。但是,我不能给xticks单个轴。这将导致一个错误:

AttributeError: 'AxesSubplot' object has no attribute 'xticks' 

回答

4

如果sharex不是主要使用此代码:

import matplotlib.pyplot as plt 
f, axs = plt.subplots(ncols=2) 
for i in range(2): 
    ax = axs[i] 
    ax.set_xticks(range(6)) 
    ax.set_xticklabels([str(x)+"foo" for x in range(6)], rotation=45) 
    ax.plot(range(6), range(6)) 
plt.show() 

enter image description here

+0

我生活中可以没有sharex。谢谢! – qznc