-
Notifications
You must be signed in to change notification settings - Fork 31
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
XFabian
wants to merge
3
commits into
pytorn:dev
Choose a base branch
from
XFabian:patch
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| 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.save("/home/xaver/graphs/scatter") | ||
| #graph1.show() | ||
| print("Fuck you Xaver") | ||
|
|
||
| graph1.choose_figure(line_graph) | ||
| graph1.save("/home/xaver/graphs/line") | ||
| #graph1.show() | ||
|
|
||
| graph1.choose_figure(bar_graph) | ||
| graph1.save("/home/xaver/graphs/bar") | ||
| #graph1.show() | ||
| """ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,4 @@ Faker>=0.8.4, <0.9 | |
| requests>=2.18.4, <2.19 | ||
| qrcode | ||
| pillow | ||
| xmljson | ||
| matplotlib | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please remove such comments? 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh of course xD