2015-05-10 34 views
3

我正在嘗試使用twinx()來創建一個條形/線條組合圖,並在條形頂部顯示這條線。目前,這是它如何出現:頂部帶有直線的多軸圖形。 Matplotlib

Bar/Line Combo

我還需要線圖上左邊的垂直軸(AX)和右邊(AX2)條被繪製,因爲它是目前。如果我上繪製它的頂部出現在第二軸的線條,但顯然它出現在了錯誤的軸(右)

這裏是我的代碼:

self.ax2=ax.twinx() 
    df[['Opportunities']].plot(kind='bar', stacked=False, title=get_title, color='grey', ax=self.ax2, grid=False) 
    ax.plot(ax.get_xticks(),df[['Percentage']].values, linestyle='-', marker='o', color='k', linewidth=1.0) 
    lines, labels = ax.get_legend_handles_labels() 
    lines2, labels2 = self.ax2.get_legend_handles_labels() 
    ax.legend(lines + lines2, labels + labels2, loc='lower right') 

還分別具有與標籤的麻煩,但一一次一件事。

回答

3

默認情況下,藝術家出現在ax上,然後 藝術家在雙軸ax2上。所以,因爲在你的代碼中,線條圖是在ax上繪製的,而條形圖在ax2上,條形圖位於線條的頂部(並且模糊了)。

(我想我可以通過指定zorder改變這一點,但嘗試沒有 工作...)

所以要解決這個問題的方法之一是使用ax繪製條形圖和ax2畫該線。這將把線放在條的上面。此外,默認情況下,將左側的ax(條形圖)的ytick標籤和右側的ax2(線條)的ytick標籤放在一起。但是,您可以使用

ax.yaxis.set_ticks_position("right") 
ax2.yaxis.set_ticks_position("left") 

交換左側和右側ytick標籤的位置。


import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 
import pandas as pd 
np.random.seed(2015) 

N = 16 
df = pd.DataFrame({'Opportunities': np.random.randint(0, 30, size=N), 
        'Percentage': np.random.randint(0, 100, size=N)}, 
        index=pd.date_range('2015-3-15', periods=N, freq='B').date) 
fig, ax = plt.subplots() 

df[['Opportunities']].plot(kind='bar', stacked=False, title='get_title', 
          color='grey', ax=ax, grid=False) 
ax2 = ax.twinx() 
ax2.plot(ax.get_xticks(), df[['Percentage']].values, linestyle='-', marker='o', 
     color='k', linewidth=1.0, label='percentage') 

lines, labels = ax.get_legend_handles_labels() 
lines2, labels2 = ax2.get_legend_handles_labels() 
ax.legend(lines + lines2, labels + labels2, loc='best') 
ax.yaxis.set_ticks_position("right") 
ax2.yaxis.set_ticks_position("left") 

fig.autofmt_xdate() 
plt.show() 

產生

enter image description here


可替代地,軸的zorder可以被設置,以便繪製ax以上ax2Paul Ivanov shows how

ax.set_zorder(ax2.get_zorder()+1) # put ax in front of ax2 
ax.patch.set_visible(False) # hide the 'canvas' 
ax2.patch.set_visible(True) # show the 'canvas' 

因此,

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 
import pandas as pd 
np.random.seed(2015) 

N = 16 
df = pd.DataFrame({'Opportunities': np.random.randint(0, 30, size=N), 
       'Percentage': np.random.randint(0, 100, size=N)}, 
index=pd.date_range('2015-3-15', periods=N, freq='B').date) 
fig, ax = plt.subplots() 
ax2 = ax.twinx() 
df[['Opportunities']].plot(kind='bar', stacked=False, title='get_title', 
          color='grey', ax=ax2, grid=False) 

ax.plot(ax.get_xticks(), df[['Percentage']].values, linestyle='-', marker='o', 
     color='k', linewidth=1.0, label='percentage') 

lines, labels = ax.get_legend_handles_labels() 
lines2, labels2 = ax2.get_legend_handles_labels() 
ax.legend(lines + lines2, labels + labels2, loc='best') 

ax.set_zorder(ax2.get_zorder()+1) # put ax in front of ax2 
ax.patch.set_visible(False) # hide the 'canvas' 
ax2.patch.set_visible(True) # show the 'canvas' 

fig.autofmt_xdate() 
plt.show() 

產生同樣的結果,而無需交換由axax2扮演的角色。

+0

這太好了。非常感謝! – user2229838