2017-06-14 82 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