Skip to content

Implemented a graph class. Usage: #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions hackr/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import matplotlib.pyplot as plt

class graph():
"""
Safe your grap first before you use show it!
"""
figure = None
coordinates = {} # a dict with the coordinates or label and coordinate pair for bar graphs
# x is the key and y is the value
figures = [] # list of figures you can switch between

xlabel = "x"
ylabel = "y"
def __init__(self, xlabel="x", ylabel="y"):
"""
Initialize
:param coordinates: the coordinates for the graph
:param xlabel: label for the x axis
:param ylabel: label for the y axis
"""
self.figure = None
self.xlabel = xlabel
self.ylabel = ylabel

def save(self, file_path, extension="png"):
"""
Saves the figure of it exists to the png format
:param file_path: Where to save the figure
:param name: optional parameter if you want to create a specific name
:param extension: optional parameter for the file type
:return:
"""
if self.figure is None:
print ("You did not create a plot. Please create a plot before")
else:
try:
self.figure.savefig(file_path + "." + extension)
except IOError as e:
print e.filename + " " + e.strerror
print "Please specify a valid save_path"
print "The path should end with /"

def show(self):
"""
Show the figure if one exists
:return:
"""
figure = self.figure
if self.figure is not None:
plt.show()
else:
print("You did not create a figure. Please create a figure first")

def create_figure(self):
"""
Creates a figure on that the graphes will be drawn. Could take size as input for example
:return:
"""
self.figure = plt.figure()

def choose_figure(self, index):
"""
Choose a figure if you created more than one so you can switch between figures
:param index: the index of he figure you want
:return:
"""
try:
self.figure = self.figures[index]
except IndexError as e:
print ("Index does not exist")

def create_bar_graph(self, coordinates):
"""
Creates a bar graph with the arguments provided.
The dictionary contains as key the labels and then y coordinate as value
:return: The index number of the figure if you want to show it
"""
sp = self.figure.add_subplot(111)
sp.bar(range(len(coordinates)), coordinates.values(), align="center")
plt.xticks(range(len(coordinates)), coordinates.keys())
self.figures.append(self.figure)
self.create_figure()
return len(self.figures) - 1


def create_scatter_graph(self, coordinates):
"""
Create a scatter graph,
:param coordinates A dictionary keys are labels and the values are the x and y coodinates per label
:return:
"""
xs, ys = zip(*coordinates.values())
labels = coordinates.keys()
plt.scatter(xs, ys)
# TODO Decide if you want to use labels
for label, x, y in zip(labels, xs, ys):
plt.annotate(label, xy=(x, y))
self.figures.append(self.figure)
self.create_figure()
return len(self.figures) - 1

def create_line_graph(self, coordinates):
"""
Create a normal line graph
:param coordinates A dictionary keys are x coordinates and values are the y coordinates
:return: The index of the figure that was created
"""
x = []
y = []

for key, value in coordinates.iteritems():
x.append(key)
y.append(value)
plt.plot(x,y)
self.figures.append(self.figure)
self.create_figure()
return len(self.figures) - 1

"""
# For testing purposes
if __name__ == "__main__":
D = {u'Label1': 26, u'Label2': 17, u'Label3': 30}
graph1 = graph(2)
test = {1092268: [81, 90], 524292: [80, 80], 892456: [88, 88]}
test2 = {10: 20, 30: 40}
# First create a figure
graph1.create_figure()
scatter_graph = graph1.create_scatter_graph(test)
line_graph = graph1.create_line_graph(test2)
bar_graph = graph1.create_bar_graph(D)
graph1.choose_figure(scatter_graph)

graph1.choose_figure(line_graph)
graph1.save("/home/user/graphs/line")
#graph1.show()

graph1.choose_figure(bar_graph)
graph1.save("/home/user/graphs/bar")
#graph1.show()
"""
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ requests>=2.18.4, <2.19
qrcode
pillow
xmljson
sphinx_rtd_theme
sphinx_rtd_theme
matplotlib