0

Someone Please tell me how to plot a bar graph or line graph in python. I want to plot a bar graph with x-axis as month and y-axis with the mean values of different columns.

A Sample of my dataset:

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

For example I want that my values on y-axis and jan 2016 on x- axis and there is a bar graph on it showing the usage of different attribute of that particular month.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
user143072
  • 9
  • 1
  • 5

2 Answers2

0

You can use plotly to achieve your desired result.

Install the plotly package

sudo pip install plotly

And then import and use as needed

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')

In your case pass the month column from data frame to x and values to y as input.

Plotly documentation

Bar charts in plotly

Naresh Kumar
  • 1,706
  • 1
  • 14
  • 26
0

For the most versatile way to use data use pandas. import matplotlib.pyplot and pandas as follow:

import matplotlib.pyplot as plt
import pandas as pd

format your data in pandas like so: the header list and a list of lists for the data:

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()

This will give you the months as indices and the columns represented by a color code for each category with a legend.

manandearth
  • 804
  • 1
  • 9
  • 25