matplotlib.pyplot自定义图例

时间:2017-02-11 16:57:00

标签: python matplotlib

我有一个相当拥挤的传奇。我想让它更轻。让以下模拟数据:

import pandas as pd

Compact = pd.DataFrame(data=[['E',1,-20,0.02],
                             ['E',2,-21,0.025],
                             ['S',1,-19,0.015],
                             ['S',2,-19.3,0.012],
                             ['S0',1,-21.2,0.023],
                             ['S0',2,-20.7,0.013],
                             ['S0a',1,-21.5,0.017],
                             ['S0a',2,-20.1,0.026]
                            ],columns=['Morph','rank','R','z'])
Compact_left = pd.DataFrame(data=[['E',1,-19.8,0.023],
                                  ['E',2,-21.5,0.018],
                                  ['S',1,-18.9,0.015],
                                  ['S',2,-19.2,0.009],
                                  ['S0',1,-17.9,0.011],
                                  ['S0',2,-20.5,0.007],
                                  ['S0a',1,-17.8,0.008],
                                  ['S0a',2,-20.5,0.006]
                                 ],columns=['Morph','rank','R','z'])

我使用以下代码绘制数据:

import matplotlib.pyplot as plt

plt.scatter(Compact.loc[(Compact['Morph']=='S') & (Compact['rank'] == 1)]['z'],
            Compact.loc[(Compact['Morph']=='S') & (Compact['rank'] == 1)]['R'],  
            c='b', marker='^', 
            label = "$S$, central, selected")
plt.scatter(Compact.loc[(Compact['Morph']=='S') & (Compact['rank'] > 1)]['z'],
            Compact.loc[(Compact['Morph']=='S') & (Compact['rank'] > 1)]['R'],  
            c='b', marker='o', 
            label = "$S$, satellite, selected")
plt.scatter(Compact_left.loc[(Compact_left['Morph']=='S') & (Compact_left['rank'] == 1)]['z'],
            Compact_left.loc[(Compact_left['Morph']=='S') & (Compact_left['rank'] == 1)]['R'],  
            facecolors='none', edgecolors='b', marker='^', 
            label = "$S$, central, put aside")
plt.scatter(Compact_left.loc[(Compact_left['Morph']=='S') & (Compact_left['rank'] > 1)]['z'],
            Compact_left.loc[(Compact_left['Morph']=='S') & (Compact_left['rank'] > 1)]['R'],  
            facecolors='none', edgecolors='b', marker='o', 
            label = "$S$, satellite, put aside")

plt.scatter(Compact.loc[(Compact['Morph']=='S0') & (Compact['rank'] == 1)]['z'],
            Compact.loc[(Compact['Morph']=='S0') & (Compact['rank'] == 1)]['R'],  
            c='g', marker='^', 
            label = "$S0$, central, selected")
plt.scatter(Compact.loc[(Compact['Morph']=='S0') & (Compact['rank'] > 1)]['z'],
            Compact.loc[(Compact['Morph']=='S0') & (Compact['rank'] > 1)]['R'],  
            c='g', marker='o', 
            label = "$S0$, satellite, selected")
plt.scatter(Compact_left.loc[(Compact_left['Morph']=='S0') & (Compact_left['rank'] == 1)]['z'],
            Compact_left.loc[(Compact_left['Morph']=='S0') & (Compact_left['rank'] == 1)]['R'],  
            facecolors='none', edgecolors='g', marker='^',
            label = "$S0$, central, put aside")
plt.scatter(Compact_left.loc[(Compact_left['Morph']=='S0') & (Compact_left['rank'] > 1)]['z'],
            Compact_left.loc[(Compact_left['Morph']=='S0') & (Compact_left['rank'] > 1)]['R'],  
            facecolors='none', edgecolors='g', marker='o',
            label = "$S0$, satellite, put aside")

plt.scatter(Compact.loc[(Compact['Morph']=='E') & (Compact['rank'] == 1)]['z'],
            Compact.loc[(Compact['Morph']=='E') & (Compact['rank'] == 1)]['R'],  
            c='r', marker='^',
            label = "$E$, central, selected")            
plt.scatter(Compact.loc[(Compact['Morph']=='E') & (Compact['rank'] > 1)]['z'],
            Compact.loc[(Compact['Morph']=='E') & (Compact['rank'] > 1)]['R'],  
            c='r', marker='o', 
            label = "$E$, satellite, selected")
plt.scatter(Compact_left.loc[(Compact_left['Morph']=='E') & (Compact_left['rank'] == 1)]['z'],
            Compact_left.loc[(Compact_left['Morph']=='E') & (Compact_left['rank'] == 1)]['R'],  
            facecolors='none', edgecolors='r', marker='^',
            label = "$E$, central, put aside")            
plt.scatter(Compact_left.loc[(Compact_left['Morph']=='E') & (Compact_left['rank'] > 1)]['z'],
            Compact_left.loc[(Compact_left['Morph']=='E') & (Compact_left['rank'] > 1)]['R'],  
            facecolors='none', edgecolors='r', marker='o',
            label = "$E$, satellite, put aside")

plt.scatter(Compact.loc[(~Compact['Morph'].isin(['S','E','S0'])) & (Compact['rank'] == 1)]['z'],
            Compact.loc[(~Compact['Morph'].isin(['S','E','S0'])) & (Compact['rank'] == 1)]['R'],  
            c='y', marker='^',
            label = "Other, central, selected")                        
plt.scatter(Compact.loc[(~Compact['Morph'].isin(['S','E','S0'])) & (Compact['rank'] > 1)]['z'],
            Compact.loc[(~Compact['Morph'].isin(['S','E','S0'])) & (Compact['rank'] > 1)]['R'],  
            c='y', marker='o',
            label = "Other, satellite, selected")
plt.scatter(Compact_left.loc[(~Compact_left['Morph'].isin(['S','E','S0'])) & (Compact_left['rank'] == 1)]['z'],
            Compact_left.loc[(~Compact_left['Morph'].isin(['S','E','S0'])) & (Compact_left['rank'] == 1)]['R'],  
            facecolors='none', edgecolors='y', marker='^',
            label = "Other, central, put aside")            
plt.scatter(Compact_left.loc[(~Compact_left['Morph'].isin(['S','E','S0'])) & (Compact_left['rank'] > 1)]['z'],
            Compact_left.loc[(~Compact_left['Morph'].isin(['S','E','S0'])) & (Compact_left['rank'] > 1)]['R'],  
            facecolors='none', edgecolors='y', marker='o',
            label = "Other, satellite, put aside")


axes = plt.gca()
zinf, zsup = Compact_raw['z'].min(),Compact_raw['z'].max()
zrange = zsup - zinf
z1, z2 = zinf - 0.03*zrange, zsup + 0.03*zrange
Rinf, Rsup = [Compact_raw['R'].min(),Compact_raw['R'].max()]
Rrange = Rsup - Rinf 
R1, R2 = Rinf - 0.03*Rrange, Rsup + 0.03*Rrange
axes.set_xlim(z1, z2)
axes.set_ylim(R1, R2)
axes.invert_yaxis()

leg = plt.legend(frameon=True, loc='center right', bbox_to_anchor=(1.33, 0.5))

leg.get_frame().set_edgecolor('k')
plt.xlabel('$z$', fontsize=20)
plt.ylabel('$M_r$', fontsize=20)

plt.axvline(0.01, color='c', linestyle='dashed', linewidth = 1)
plt.axhline(R_min, color='c', linestyle='dashed', linewidth = 1);

使用我自己的数据,结果如下: enter image description here

如您所见,传说太复杂了。我想构建一个自定义图例来解释:

  • 蓝色表示S,红色表示E,绿色表示S0,黄色表示其他
  • 三角形用于中央星系,圆形用于卫星
  • 选择完整的标记星系,将空的星系放在一边

使用旧语言,我能够执行以下操作: enter image description here

这不是很漂亮,但就传说而言,效率更高一些。我尝试了各种策略,但我无法让Pyplot做一些干净的事情。我该怎么办?

0 个答案:

没有答案