2017-06-14 81 views
0

我有一個Python3.6代碼,它產生兩個圖,一個有三個軸,另一個有兩個軸。三軸的情節有一個細線的傳說。Matplotlib減少帶有兩個y軸的鍋的圖線厚度?

不幸的是,第二圖例具有厚度等於所述標籤的高度行:

enter image description here

這裏是第二圖形的代碼:在一個數字

兩個曲線

如何減少圖例中線條的粗細?

這似乎是代碼沒有達到崗位。

這就是:

#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找到。您可以使用線這樣的小例子:

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

通常的方法來創建標籤圖例是使用藝術家的label參數圖例顯示,ax.plot(...., label="some label")。使用具有此標籤集的實際藝術家確保圖例顯示與藝術家相對應的符號;在線情節的情況下,這自然是一條線。

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