【问题标题】:Pandas matplotlib.pyplot add legend by a column valuePandas matplotlib.pyplot 按列值添加图例
【发布时间】:2016-11-09 02:26:34
【问题描述】:

我正在使用 Pandas 制作散点图。我的数据如下所示:



    Locations Lenovo Global Region Primary Function Subsidiaries Apr_2015_to_Mar_2016_[kWh] Apr_2015_to_Mar_2016_[MT] color MT/kWh
<!-- -->  

    263 United Kingdom - Hook EMEA Large Office (OFL) Lenovo 7.561727e+04 129.438515 r 0.001712  
    202 South Africa - Johannesburg/Bryanston EMEA Small Office (OSL) Lenovo 1.013746e+05 93.872885 r 0.000926  
    232 India - Chennai Factory Asia Pacific Manufacturing (MFG) Motorola Mobility 3.163600e+05 271.933953 g 0.000860  
    159 India - Pondicherry Asia Pacific    Manufacturing (MFG) Lenovo  1.074016e+06    907.869649  g   0.000845  
    242 Australia - Chatswood   Asia Pacific    Large Office (OFL)  Lenovo  3.001254e+05    239.500093  g   0.000798

定义一个函数来为不同的区域使用不同的颜色。

def colorpoint(row):
    if row['Lenovo Global Region'] == 'Asia Pacific':
        return('g')
    if row['Lenovo Global Region'] == 'EMEA':
        return('r')
    else:
        return('b')
test3['color'] = test3.apply (lambda row: colorpoint (row),axis=1)

定义我想要绘制的散点。

y=test3['Apr_2015_to_Mar_2016_[MT]']
x=test3['Apr_2015_to_Mar_2016_[kWh]']
T = test3['color']
area= (y/x)*500000
xmax=1.1*max(test3['Apr_2015_to_Mar_2016_[kWh]'])
ymax=1.1*max(test3['Apr_2015_to_Mar_2016_[MT]'])

绘制图形。 fig1 = plt.figure(figsize=(16,9), dpi=300) ax = plt.subplot(111)

plot=plt.scatter(x,y,alpha=0.6,c=T,s=area)
ax.grid(True)
ax.set_xlim([0,xmax])
ax.set_ylim([0,ymax])
ax.set_xlabel('Apr 2015 to Mar 2016 [kWh]')
ax.set_ylabel('Apr 2015 to Mar 2016 [MT]')
ax.set_title('Total Elec consumtion [kWh] VS CO2 emission [MT]')

尝试添加图例。 我想显示与他们的“联想全球区域”相对应的颜色,但它不起作用,只将一个区域“美国集团”显示为蓝点

legend=test3['Lenovo Global Region']
plt.legend(legend,loc=4)

如果您有想法,谢谢!

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    也许这就是你在最后一行所需要的:

    plt.legend(legend.values,loc=4)
    

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      for city,color in [('Asia Pacific', 'Green'), ('EMEA', 'Red'), ('rest', 'Blue')]:
          x = test3.loc[test3['Lenovo Global Region']==city]['Apr_2015_to_Mar_2016_[kWh]']
          y = test3.loc[test3['Lenovo Global Region']==city]['Apr_2015_to_Mar_2016_[MT]']
          area= (y/x)*500000
          plt.scatter(x, y, alpha=0.6,c=color,s=area, label=city)
      plt.legend()
      

      【讨论】:

        【解决方案3】:

        来自可能相同的另一个问题: Matplotlib adding legend based on existing color series

        “您可以使用空图创建图例句柄,其颜色基于散点图的颜色图和标准化。”

        sc = plt.scatter(df['x'], df['y'], s=size, c=df['colors'], edgecolors='none')
        
        lp = lambda i: plt.plot([],color=sc.cmap(sc.norm(i)), ms=np.sqrt(size), mec="none",
                                label="Feature {:g}".format(i), ls="", marker="o")[0]
        handles = [lp(i) for i in np.unique(df["colors"])]
        plt.legend(handles=handles)
        plt.show()
        

        因此,从他们的代码到您的代码,只需将您的 plt.scatter 分配给 sc 之类的变量,然后将所有 df['colors'] 替换为 T。

        【讨论】:

          猜你喜欢
          • 2014-03-30
          • 1970-01-01
          • 1970-01-01
          • 2018-09-17
          • 2018-04-08
          • 1970-01-01
          • 2021-12-02
          • 2022-01-21
          • 1970-01-01
          相关资源
          最近更新 更多