2017-09-18 14 views
0

私はJupiter Notebookで複合凡例を作成しようとしています。例からさまざまなコードを試してみると、私は空の伝説を得ます。例は正常にコピーされますが、私自身のコードに実装すると何かがうまくいかなくなります。何か案は? 結果:
Result of presented code コード:Python:Matplotlib Subplotの複合凡例

fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, figsize=(15,10)) 
l1 = ax1.plot(time[18206:18226],tpm2[18206:18226], 'r', label='Chilbolton 2') 
ax1.set_title('Difference in Hydrometeor Count Per Minute Over Time') 
ax1.set_ylim([0,14000]) 
ax1.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5) 

l2 = ax2.plot(time[18206:18226],tpm1[18206:18226], 'b', label='Chilbolton 2') 
ax2.set_ylim([0,14000]) 
ax2.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5) 

l3 = ax3.plot(time[18206:18226],diff[18206:18226], 'k', label='D.P.M.') 
ax3.plot(time[18206:18226],np.zeros(20),'k--') 
ax3.set_xlabel('Time (10th February to 29th April)') 
ax3.set_ylim([-3000,3000]) 
ax3.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5) 

#plt.legend(handles=[l1, l2, l3], labels=['l1','l2','l3'],loc="upper left", bbox_to_anchor=[0, 1], 
#   ncol=2, shadow=True, title="Legend", fancybox=True) 
fig.legend((l1, l2, l3), ('Line 1', 'Line 2', 'Line 3'), 'upper left') 
# ('Chilbolton 2','Chilbolton 2','D.P.M.'), loc = (0.5, 0), ncol=1) 

plt.ylabel('Hydrometeor Count (#)') 
# Fine-tune figure; make subplots close to each other and hide x ticks for 
# all but bottom plot. 
#f.subplots_adjust(hspace=0) 
plt.setp([a.get_xticklabels() for a in f.axes[-1:]], rotation=90, visible=True) 
plt.show() 

答えて

0

回避策として、あなたはPatchを使用して、独自のカスタム凡例を作成することができます。

import numpy as np    
import matplotlib.pyplot as plt 
import matplotlib.patches as mpatches 
import random 

tx = range(20) 
t1 = np.random.randint(0, 14000, 20) 
t2 = np.random.randint(0, 14000, 20) 
t3 = np.random.randint(-3000, 3000, 20) 

labels = ['Chilbolton 2', 'Chilbolton 2', 'D.P.M.'] 

fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, figsize=(15,10)) 
l1 = ax1.plot(tx, t1, 'r', label=labels[0]) 
ax1.set_title('Difference in Hydrometeor Count Per Minute Over Time') 
ax1.set_ylim([0,14000]) 
ax1.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5) 

l2 = ax2.plot(tx,t2, 'b', label=labels[1]) 
ax2.set_ylim([0,14000]) 
ax2.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5) 

l3 = ax3.plot(tx,t3, 'k', label=labels[2]) 
ax3.plot(tx,np.zeros(20),'k--') 
ax3.set_xlabel('Time (10th February to 29th April)') 
ax3.set_ylim([-3000,3000]) 
ax3.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5) 

# Create custom legend 
leg1 = mpatches.Patch(color='r') 
leg2 = mpatches.Patch(color='b') 
leg3 = mpatches.Patch(color='k') 
fig.legend(handles=[leg1, leg2, leg3], labels=labels, loc="upper left") 

plt.ylabel('Hydrometeor Count (#)') 
# Fine-tune figure; make subplots close to each other and hide x ticks for 
# all but bottom plot. 
#f.subplots_adjust(hspace=0) 
plt.setp([a.get_xticklabels() for a in fig.axes[-1:]], rotation=90, visible=True) 
plt.show() 

はあなたに与える:

Demo legend

1

ax.plot()は、1行しかプロットしていない場合でも、リストのラインアーティストを返します。したがって、l1 = ax1.plot(...)と書くと、長さ-1のリストがl1に割り当てられます。同義語についてはl2l3です。これにより、fig.legend()の問題が発生します。これにはラインアーティストオブジェクトだけが必要です。

この問題は、さまざまな方法で解決できます。カンマを挿入

l1, = ax1.plot(... 

l1へ返されるリストの要素のみを割り当てます。最も一般的に使用される方法のような構文は次のとおりです。 l1 = ax1.plot(...)[0]もできます。または、あなたの場合、凡例の呼び出しをfig.legend((l1[0],l2[0],l3[0]),...)に変更することができます。

ので、

import maptlotlib.pyplot as plt 

fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, figsize=(15,10)) 

l1, = ax1.plot([0,1],[0,14000]) 
ax1.set_title('Difference in Hydrometeor Count Per Minute Over Time') 
ax1.set_ylim([0,14000]) 
ax1.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5) 

l2, = ax2.plot([0,1],[0,14000]) 
ax2.set_ylim([0,14000]) 
ax2.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5) 

l3, = ax3.plot([0,1],[-3000,3000]) 
ax3.plot(time[18206:18226],np.zeros(20),'k--') 
ax3.set_xlabel('Time (10th February to 29th April)') 
ax3.set_ylim([-3000,3000]) 
ax3.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5) 

fig.legend((l1, l2, l3), ('Line 1', 'Line 2', 'Line 3'), 'upper left') 

enter image description here