首页 文章

绘制python中的条形图[重复]

提问于
浏览
0

这个问题在这里已有答案:

有人请告诉我如何在python中绘制条形图或折线图 . 我想绘制一个条形图,其中x轴为月和y轴,具有不同列的平均值 .

我的数据集的示例:

Month   Bedroom Outlets Cellar Outlets  Bedroom Lights  DisposalDishwasher  DuctHeaterHRV   FridgeRange FurnaceHRV  KitchenLights   MasterLights    MasterOutlets   WashingMachine
Jan-16  0.008704    0.074089    0.006081    0.000116    0.000058    0.001162    0.176832    0.000024    0.014887    0.009617    0.000378
Feb-16  0.008187    0.075153    0.005993    0.000102    0.000059    0.001905    0.172289    0.000023    0.01448 0.007724    0.000367
Mar-16  0.007725    0.072855    0.005536    0.000073    0.000048    0.001469    0.1261  0.000015    0.014242    0.005848    0.00024
Apr-16  0.007678    0.074465    0.005729    0.000061    0.000042    0.001129    0.093861    0.000014    0.014267    0.005899    0.000152
May-16  0.007864    0.075408    0.005823    0.000096    0.000102    0.001691    0.116811    0.000029    0.014387    0.007111    0.000406
Jun-16  0.006876    0.07829 0.005587    0.000143    0.000134    0.000937    0.176654    0.000046    0.014229    0.005706    0.000654
Jul-16  0.006032    0.093383    0.006214    0.000193    0.000236    0.000831    0.228637    0.000082    0.014352    0.005174    0.001004

例如,我希望我的值在y轴和2016年1月在x轴上,并且在其上有一个条形图,显示该特定月份的不同属性的使用 .

2 回答

  • 0

    您可以使用plotly来实现所需的结果 .

    安装plotly包

    sudo pip install plotly
    

    然后根据需要导入和使用

    import plotly.plotly as py
    import plotly.graph_objs as go 
    
    data = [go.Bar( x=['jan', 'feb', 'mar'], y=[20, 14, 23] )] 
    py.iplot(data, filename='basic-bar')
    

    在您的情况下,将月份列从数据框传递到x,将值传递给y作为输入 .

    Plotly documentation

    Bar charts in plotly

  • 0

    对于使用数据最通用的方式使用pandas . 导入matplotlib.pyplot和pandas如下:

    import matplotlib.pyplot as plt
    import pandas as pd
    

    像这样在pandas中格式化数据: Headers 列表和数据列表列表:

    data = ['Month', 'Bedroom Outlets', 'Cellar Outlets', 'Bedroom Lights', 'DisposalDishwasher', 'DuctHeaterHRV', 'FridgeRange', 'FurnaceHRV', 'KitchenLights', 'MasterLights', 'MasterOutlets', 'WashingMachine']
    list_01 = [['Jan-16', 0.008704, 0.074089, 0.006081, 0.000116, 0.000058, 0.001162, 0.176832, 0.000024, 0.014887, 0.009617, 0.000378], ['Feb-16', 0.008187, 0.075153, 0.005993, 0.000102, 0.000059, 0.001905, 0.172289, 0.000023, 0.01448, 0.007724, 0.000367], ['Mar-16', 0.007725, 0.072855, 0.005536, 0.000073, 0.000048, 0.001469, 0.1261, 0.000015, 0.014242, 0.005848, 0.00024], ['Apr-16', 0.007678, 0.074465, 0.005729, 0.000061, 0.000042, 0.001129, 0.093861, 0.000014, 0.014267, 0.005899, 0.000152], ['May-16', 0.007864, 0.075408, 0.005823, 0.000096, 0.000102, 0.001691, 0.116811, 0.000029, 0.014387, 0.007111, 0.000406], ['Jun-16', 0.006876, 0.07829, 0.005587, 0.000143, 0.000134, 0.000937, 0.176654, 0.000046, 0.014229, 0.005706, 0.000654], ['Jul-16', 0.006032, 0.093383, 0.006214, 0.000193, 0.000236, 0.000831, 0.228637, 0.000082, 0.014352, 0.005174, 0.001004]]
    data_frame = pd.DataFrame(list_01, columns=data)
    data_frame2 = data_frame.set_index(['Month'])
    data_frame2.plot.bar()
    plt.show()
    

    这将为您提供月份作为索引和由带有图例的每个类别的颜色代码表示的列 .

相关问题