在python中修改matplolib图例 [英] Modify matplolib legend in python

查看:47
本文介绍了在python中修改matplolib图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用以下代码绘制我的数据.:

<预><代码>##绘图plt.plot(temp1, densityHEX,'bo',markersize='12',label='十六烷 MD')plt.plot(expTempHexa,expDensHexa,'bo',markersize ='12',markerfacecolor ='none',label ='Hexadecane Exp')plt.plot(temp2,densityLAU,'rs',markersize ='12',label ='月桂酸甲酯MD')plt.plot(expTempLau2,expDensLau2,'rs',markersize='12',markerfacecolor='none',label='月桂酸甲酯 Exp')plt.plot(temp2, densityMIX,'^',color='indigo',markersize='12',label='Mixture 1:1 MD')plt.plot(expTempMix,expDensMix,'^',color='indigo',markersize='12',markerfacecolor='none',label='Mixture 1:1 Exp')leg = plt.legend(loc='best',ncol=2,frameon=False,prop={'size': 13})leg.get_frame().set_edgecolor('k')plt.ylabel(r'$\rho$ (g/cm$^3$)',fontsize = 22)plt.xlabel('T (K)',fontsize = 22)plt.ylim((0.650,1.0))plt.tight_layout()plt.savefig("rhoCombined3.pdf", format='pdf')

输出是这样的:

我想知道是否有办法修改图例以获得这样的东西:

.

解决方案

一种选择是:

将 numpy 导入为 np导入matplotlib.pyplot作为plt从matplotlib.legend_handler导入HandlerTuplex = np.arange(10)y = x * 0.2#绘图l1,= plt.plot(x,y + 0.0,'bo',markersize = '12',label ='十六进制MD')l2,= plt.plot(x,y + 0.3,'bo',markersize = '12',markerfacecolor ='none',label ='Hexadecane Exp')l3 = plt.plot(x,y + 1.0,'rs',markersize = '12',label ='月桂酸甲酯MD')l4,= plt.plot(x,y + 1.3,'rs',markersize = '12',markerfacecolor ='none',label ='Murate Laurate Exp')l5, = plt.plot(x,y+2.0, '^', color='indigo', markersize='12', label='Mixture 1:1 MD')l6,= plt.plot(x,y + 2.3,'^',color ='indigo',markersize = '12',markerfacecolor ='none',label ='Mixture 1:1 Exp')plt.legend(handles = [(l1,l2),(l3,l4),(l5,l6)],标签=[十六烷"、月桂酸甲酯"、混合物 1:1"]、handler_map={tuple: HandlerTuple(ndivide=2)},handlelength = 3,title ="MD Exp")plt.show()

误用列标题"的标题有点脏.但是它很快就能提供所需的输出.

I currently use the following code to plot my data.:

#
#Plotting
plt.plot(temp1, densityHEX,'bo',markersize='12',label='Hexadecane MD')
plt.plot(expTempHexa,expDensHexa,'bo',markersize='12',markerfacecolor='none',label='Hexadecane Exp')

plt.plot(temp2, densityLAU,'rs',markersize='12',label='Methyl Laurate MD')
plt.plot(expTempLau2,expDensLau2,'rs',markersize='12',markerfacecolor='none',label='Methyl Laurate Exp')

plt.plot(temp2, densityMIX,'^',color='indigo',markersize='12',label='Mixture 1:1 MD')
plt.plot(expTempMix,expDensMix,'^',color='indigo',markersize='12',markerfacecolor='none',label='Mixture 1:1 Exp')

leg = plt.legend(loc='best',ncol=2,frameon=False,prop={'size': 13})
leg.get_frame().set_edgecolor('k')
plt.ylabel(r'$\rho$ (g/cm$^3$)',fontsize = 22)
plt.xlabel('T (K)',fontsize = 22)
plt.ylim((0.650,1.0))
plt.tight_layout()
plt.savefig("rhoCombined3.pdf", format='pdf')

The output is this:

I was wondering if there is a way to modify the legend to obtain something like this instead:

.

解决方案

One option is this:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerTuple

x = np.arange(10)
y = x*0.2

#Plotting
l1, = plt.plot(x,y+0.0, 'bo', markersize='12', label='Hexadecane MD')
l2, = plt.plot(x,y+0.3, 'bo', markersize='12', markerfacecolor='none', label='Hexadecane Exp')

l3, = plt.plot(x,y+1.0, 'rs', markersize='12', label='Methyl Laurate MD')
l4, = plt.plot(x,y+1.3, 'rs', markersize='12', markerfacecolor='none', label='Methyl Laurate Exp')

l5, = plt.plot(x,y+2.0, '^', color='indigo', markersize='12', label='Mixture 1:1 MD')
l6, = plt.plot(x,y+2.3, '^', color='indigo', markersize='12', markerfacecolor='none',label='Mixture 1:1 Exp')

plt.legend(handles=[(l1, l2), (l3, l4), (l5, l6)], 
           labels=["Hexadecane", "Methyl Laurate", "Mixture 1:1"],
           handler_map={tuple: HandlerTuple(ndivide=2)}, 
           handlelength=3,
           title="MD  Exp                         ")
plt.show()

Misusing the title for the "column header" is a bit of a dirty hack. But it quickly gives the desired output.

这篇关于在python中修改matplolib图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆