-
Notifications
You must be signed in to change notification settings - Fork 0
Section 2: Graphing
in pryttier.graphing
This module makes plotting graphs a lot easier. It uses matplotlib. The code is a lot more readable and appealing.
Creating a graph is simple. The below code creates a 2D graph:
graph = Graph2D()
graph.addAxes()
graph.show()Let's break the down the above code:
-
The first line creates an instance of class
Graph2d. It has 2 parameters (optional): name and style. The name will be the title, it defaults to "Graph 2D". We will talk about style later. -
The second line adds a 2d axes to the figure
-
The last line finally shows the graph. All the graphing and plotting stuff will be between the second line and the last line.
Let's look at what can we graph.
Remember the Vector2 class? You can use it to plot points. You can also give it a color and a label which will be later used for showing legend.
Example:
graph = Graph2D()
graph.addAxes()
graph.plotPoint(Vector2(1, 3)) # default color blue and no label
graph.plotPoint(Vector2(1, 4), color="orange") # color can be names
graph.plotPoint(Vector2(2, 4), color="#2251f8") # or hex
graph.plotPoint(Vector2(1, 1), label="Point") # labelled point
graph.show()Output:

This function is for plotting multiple points with all having same color and label.
Example:
graph = Graph2D()
graph.addAxes()
a = Vector2(1, 2)
b = Vector2(2, 3)
c = Vector2(3, 4)
graph.plotPoints(a, b, c, color="red")
graph.show()Output:

3. drawLine(self, start: Vector2, end: Vector2, color: str = 'blue', linewidth: float = 2, linestyle: str = "-", label: str = None)
This function simple draws a line between 2 given points. It can be given a color, strok width, stroke style (matplotlib) and a label.
Example:
graph = Graph2D()
graph.addAxes()
a = Vector2(1, 2)
b = Vector2(2, 3)
graph.drawLine(a, b, color='green', linewidth=5, linestyle=".-")
graph.show()Output:

Heres all the linestyles from matplotlib
