Intelligent Energy Management Systems
Data Plotting and Visualization
September 8, 2019
1 Data Plotting and Visualization
Simple Plotting
Bar Chart
Histogram
Pie Chart
Plot Curve of Equations
Scatter Plot
[1]: #Simple Plotting
# importing the required module
import matplotlib.pyplot as plt
# x axis values
x = [1,2,3,4,5]
# corresponding y axis values
y = [2,4,1,3,4]
# plotting the points
plt.plot(x, y)
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('Simple Plotting')
# function to show the plot
plt.show()
<Figure size 640x480 with 1 Axes>
[2]: %matplotlib inline
1
[3]: #BAR CHART
import matplotlib.pyplot as plt
# x-coordinates of left sides of bars
left = [1, 2, 3, 4, 5]
# heights of bars
height = [10, 24, 36, 40, 23]
# labels for bars
tick_label = ['one', 'two', 'three', 'four', 'five']
# plotting a bar chart
plt.bar(left, height, tick_label = tick_label,
width = 0.8, color = ['blue', 'green'])
# naming the x-axis
plt.xlabel('x - axis')
# naming the y-axis
plt.ylabel('y - axis')
# plot title
plt.title(' Bar chart')
# function to show the plot
plt.show()
2
[4]: #HISTOGRAM
import matplotlib.pyplot as plt
# load frequencies
load = [2,5,70,40,30,45,50,45,43,40,44,
60,7,13,57,18,90,77,32,21,20,40]
# setting the ranges and no. of intervals
range = (0, 70)
bins = 7
# plotting a histogram
plt.hist(load, bins, range, color = 'green',
histtype = 'bar', rwidth = 0.8)
# x-axis label
plt.xlabel('Hours')
# frequency label
plt.ylabel('Weekly ')
# plot title
plt.title('Consumption of Electricity')
# function to show the plot
3
plt.show()
[5]: %matplotlib inline
[6]: #PIE CHART
import matplotlib.pyplot as plt
# defining labels
Load_consumption = ['East', 'South', 'West', 'North']
# portion covered by each label
slices = [3, 7, 8, 6]
# color for each label
colors = ['r', 'y', 'g', 'b']
# plotting the pie chart
plt.pie(slices, labels = Load_consumption, colors=colors,
startangle=90, shadow = True, explode = (0, 0, 0.1, 0),
radius = 1.2, autopct = '%1.1f%%')
# plotting legend
4
#plt.legend()
# showing the plot
plt.show()
[7]: #Plotting curves of given equation
# importing the required modules
import matplotlib.pyplot as plt
import numpy as np
# setting the x - coordinates
x = np.arange(0, 2*(np.pi), 0.1)
# setting the corresponding y - coordinates
y = np.sin(x)
# potting the points
plt.plot(x, y)
# function to show the plot
plt.show()
5
[8]: #Plotting two or more lines on same plot
import matplotlib.pyplot as plt
# line 1 points
x1 = [1,2,3]
y1 = [2,4,1]
# plotting the line 1 points
plt.plot(x1, y1, label = "line 1")
# line 2 points
x2 = [1,2,3]
y2 = [4,1,3]
# plotting the line 2 points
plt.plot(x2, y2, label = "line 2")
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('Two lines on same graph!')
# show a legend on the plot
plt.legend()
6
# function to show the plot
plt.show()
[9]: #Scatter plot
import matplotlib.pyplot as plt
# x-axis values
x = [1,2,3,4,5,6,7,8,9,10]
# y-axis values
y = [2,4,5,7,6,8,9,11,12,12]
# plotting points as a scatter plot
plt.scatter(x, y, label= "stars", color= "green",
marker= "*", s=30)
# x-axis label
plt.xlabel('x - axis')
# frequency label
plt.ylabel('y - axis')
# plot title
plt.title('My scatter plot!')
7
# showing legend
plt.legend()
# function to show the plot
plt.show()
2 References:
https://matplotlib.org/
https://github.com/