2016-05-16 90 views

回答

2

你將不得不建立一些像兩個地塊,並使用x軸的其中之一是這個二次軸。您的情節中還有其他功能,比如設置蜱和他們的位置的標籤。下面的示例播放有點用這些元素,所以你應該能夠適應您的問題:

import numpy as np 
import matplotlib.pyplot as plt 

fig = plt.figure() 
ax1 = fig.add_subplot(111) 
ax2 = ax1.twiny() 

# Add some extra space for the second axis at the bottom 
fig.subplots_adjust(bottom=0.2) 
ax1.set_xticks([1,2,4,5,7,8]) 
ax1.set_xlim(0,9) 
ax1.set_xticklabels(('2015','2016','2015','2016','2015','2016')) 
ax2.spines["bottom"].set_position(("axes", -0.15)) 
ax2.xaxis.set_ticks_position("bottom") 
ax2.spines["bottom"].set_visible(True) 
ax2.set_xticks([1.5,4.5,7.5]) 
ax2.set_xticklabels(('1','2','3')) 
ax2.set_xlim(0,9) 

b1 = np.random.randint(0,100,6) 
b2 = np.random.randint(0,100,6) 
b3 = np.random.randint(0,100,6) 
plt.bar(np.array([1,2,4,5,7,8])-0.4,b1,color='blue') 
plt.bar(np.array([1,2,4,5,7,8])-0.4,b2,color='orange',bottom=b1) 
plt.bar(np.array([1,2,4,5,7,8])-0.4,b3,color='yellow',bottom=b1+b2) 

plt.show() 

,結果是這樣的:

Two axis in a stacked bar plot in matplotlib

相關問題