【问题标题】:python subplot plot.bar from one dataframe and legend from a different dataframepython subplot plot.bar 来自一个数据框和图例来自不同的数据框
【发布时间】:2019-01-24 22:32:12
【问题描述】:

我下面有两个数据集

Df1:

    Cluster     HPE     FRE     UNE
0        0  176617  255282   55881
1        1  126130    7752  252045
2        2   12613   52326    7434

我画了一个条形图。 (这不是我的确切代码,但它会给你一个想法)

Hd=list(Df1.columns)
for i in range(1,4):
  subp=Fig.add_subplot(3,1,i) 
  plt.bar(Df1[Hd[0]],DataFrame[Hd[i]],width=0.4)

现在我想要一个基于第二个质心数据集的图例。

Df2:

   Cluster          HPE         FRE          UNE
0        0    19.282091  106.470162  1620.005037
1        1  1790.500000  367.625000   537.856177
2        2  1500.000000  180.148148  4729.275913

HPE 子图应具有 HPE 列值 (19.282091,1790.500000,1500.000000) 如下。

我该怎么做?

【问题讨论】:

    标签: python pandas dataframe matplotlib legend


    【解决方案1】:

    将图例显式设置为从第二个数据帧获得的字符串(如果您想要条形的颜色框):

    subp.legend([str(a) + ' - ' + str(b) for a, b in zip(df2['Cluster'].tolist(), df2['HPE'].tolist())])
    

    或者只使用一张桌子:

    plt.table(cellText=df2[['Cluster', 'HPE']].values, loc='upper right') 
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 2019-02-11
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    相关资源
    最近更新 更多