2016-05-16 3 views

Antwort

2

Sie werden so etwas wie zwei Parzellen haben und die x-Achse für eine von ihnen verwenden, um bauen diese sekundäre Achse zu sein. Es gibt andere Funktionen in Ihrem Plot, wie zum Beispiel die Beschriftungen für die Ticks und ihre Positionen. Das folgende Beispiel ein wenig mit jedem dieser Elemente spielt so sollten Sie in der Lage sein, um Ihr Problem zu adaptieren:

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() 

, ist das Ergebnis folgendermaßen aus:

Two axis in a stacked bar plot in matplotlib

Verwandte Themen