2017-06-14 12 views
0

私は、3つの軸と2つの軸を持つ2つのプロットを生成するPython3.6コードを持っています。 3軸のプロットには、細い線で凡例が表示されます。 2番目の図のコードである。ここでMatplotlib 2つのy軸を持つポットのプロット凡例線の太さを減らしますか?

enter image description here

残念ながら第二プロット凡例は、ラベルの高さに等しい厚さを有する線有する一桁上の

2のプロットを

凡例の線の太さをどのように減らすことができますか?

このコードは投稿に表示されなかったようです。

ここにある:あなたの凡例の行をしたいが、実際にパッチを使用よう

#two plots on one figure 

def two_scales(ax1, time, data1, data2, c1, c2): 

    ax2 = ax1.twinx() 

    ax1.plot(time, data1, 'r') 
    ax1.set_xlabel("Distance ($\AA$)") 
    ax1.set_ylabel('Atom Charge',color='r') 

    ax2.plot(time, data2, 'b') 
    ax2.set_ylabel('Orbital Energy',color='b') 
    return ax1, ax2 



t = data[:,0] 
s1 = data[:,3] 
s2 = data[:,5] 

fig, ax = plt.subplots() 
ax1, ax2 = two_scales(ax, t, s1, s2, 'r', 'b') 


def color_y_axis(ax, color): 
    for t in ax.get_yticklabels(): 
     t.set_color(color) 
    return None 
color_y_axis(ax1, 'r') 
color_y_axis(ax2, 'b') 

plt.title('Molecular Transforms') 

patch_red = mpatches.Patch(color='red',label='Atom Charge') 
patch_blue = mpatches.Patch(color='blue',label='Orbital Energy') 
plt.legend(handles = [patch_red,patch_blue]) 

plt.draw() 
plt.show() 

name_plt = name+'-fig2.png' 
fig.savefig(name_plt,bbox_inches='tight') 
+0

ソリューションのための多くのおかげで。問題は解決されました。 – Steve

答えて

1

が鳴ります。例と詳細はhereです。伝説のラベルを作成するための通常の方法は、伝説、ax.plot(...., label="some label")に表示するアーティストのlabel引数を使用することです

import matplotlib.lines as mlines 
import matplotlib.pyplot as plt 

line_red = mlines.Line2D([], [], color='red',label='Atom Charge') 
line_blue = mlines.Line2D([], [], color='blue',label='Orbital Energy') 
plt.legend(handles = [line_red,line_blue]) 
plt.show() 

enter image description here

0

:あなたはこの小さな例のような行を使用することができます。このラベルが設定されている実際のアーティストを使用して、凡例にアーティストに対応する記号が表示されていることを確認します。ラインプロットの場合、これは当然のことながらラインになります。

import matplotlib.pyplot as plt 

time=[1,2,3,4]; data1=[0,5,4,3.3];data2=[100,150,89,70] 

fig, ax1 = plt.subplots() 
ax2 = ax1.twinx() 

l1, = ax1.plot(time, data1, 'r', label='Atom Charge') 
ax1.set_xlabel("Distance ($\AA$)") 
ax1.set_ylabel('Atom Charge',color='r') 

l2, = ax2.plot(time, data2, 'b', label='Orbital Energy') 
ax2.set_ylabel('Orbital Energy',color='b') 


ax1.legend(handles=[l1,l2]) 

plt.show() 

enter image description here

関連する問題