代码之家  ›  专栏  ›  技术社区  ›  Gonzalo

基于数据帧制作2个变量的条形图

  •  4
  • Gonzalo  · 技术社区  · 7 年前

    我试图绘制两个变量的图,但没有成功。我的目标是创建一个条形图,其中事件1和事件2出现在一起,以便更容易比较“名称”(每个名称两个条形图)。我下面的代码错误地生成了两个图。你能告诉我做这件事的正确方法吗?谢谢

    我的df是:

             Name  Events1 Events2
    0  Accounting        3       3
    1   Reporting        1       4
    2     Finance        1      13
    3       Audit        1      17
    4    Template        2      40
    

    import matplotlib.pyplot as plt
    ax = df[['Events1','Events2']].plot(kind='bar', title ="test", figsize=(15, 10), legend=True, fontsize=12)
    ax.set_xlabel("Name", fontsize=12)
    ax.set_ylabel("Number", fontsize=12)
    plt.show()
    
    2 回复  |  直到 7 年前
        1
  •  4
  •   Vaishali    7 年前

    df.plot.bar(x = 'Name', y = ['Events1', 'Events2'], rot = 40)
    

    fig, ax = plt.subplots()
    df.plot.bar(x = 'Name', y = ['Events1', 'Events2'], rot = 40, ax = ax)
    for p in ax.patches: 
        ax.annotate(np.round(p.get_height(),decimals=2), (p.get_x()+p.get_width()/2., p.get_height()))
    

    enter image description here

        2
  •  2
  •   Zero    7 年前

    使用 set_index

    In [3596]: df.set_index('Name').plot.bar()
    Out[3596]: <matplotlib.axes._subplots.AxesSubplot at 0x2954d208>
    

    enter image description here


    In [3595]: df.set_index('Name')
    Out[3595]:
                Events1  Events2
    Name
    Accounting        3        3
    Reporting         1        4
    Finance           1       13
    Audit             1       17
    Template          2       40