Graph Plotter
Plotter
- class curvipy.Plotter(screen_config: Optional[ScreenConfiguration] = None, plotting_config: Optional[PlottingConfiguration] = None, axes_config: Optional[AxesConfiguration] = None)
Graph plotter for drawing curves and vectors.
- Parameters
screen_config (ScreenConfiguration) – Screen configuration. By default, screen_config takes the defaults attributes values of ScreenConfiguration.
plotting_config (PlottingConfiguration) – Plotting configuration. By default, plotting_config takes the defaults attributes values of PlottingConfiguration.
axes_config (AxesConfiguration) – Axes configuration. By default, axes_config takes the defaults attributes values of AxesConfiguration.
- clean() None
Removes curves and vectors plotted.
- draw_x_tick(number: Union[int, float], align: Optional[str] = None) None
Draws a tick on the x-axis.
- Parameters
number (int or float) – Tick number.
align (str) – Can either be “top” or “down”. Defines if the x-axis tick number will be placed upside or downside the y-axis. If None, align takes Plotter.x_ticks_align attribute value. Defaults to None.
- draw_y_tick(number: Union[int, float], align: Optional[str] = None) None
Draws a tick on the y-axis.
- Parameters
number (int or float) – Tick number.
align (str) – Can either be “left” or “right”. Defines if the y-axis ticks number will be placed to the left or to the right of the x-axis. If None, align takes Plotter.y_ticks_align attribute value. Defaults to None.
- plot_animated_curve(curve: Curve, samples_per_vector: int) None
Plots the given curve by drawing a set of vectors pointing at the curve points and then joining the vector heads.
- Parameters
curve (Curve) – Curve to be plotted.
samples_per_vector (int) – Number of samples per each vector plotted. The less samples_per_vector is, the more vectors are drawn.
- plot_curve(curve: Curve) None
Plots the given two-dimensional curve in the specified interval.
- Parameters
curve (Curve) – Curve to be plotted.
- plot_vector(vector: Vector) None
Plots the given two-dimensional vector.
- Parameters
vector (Vector) – Vector to be plotted.
- wait() None
Waits until plotter screen is clicked. When clicked, exits plotter.
Screen Configuration
- class curvipy.ScreenConfiguration(window_title: str = 'Curvipy', background_color: str = '#FFFFFF', window_width: Optional[int] = None, window_height: Optional[int] = None)
Defines the configuration for the plotter screen.
- Parameters
window_title (str) – Title to display on window. Defaults to “Curvipy”.
background_color (str) – Background color. Can either be a name or a hex color code. Defaults to “#FFFFFF”.
window_width (int or None) – Width of the screen window (in pixels). If None, window_width equals to 50% of the display width.
window_height (int or None) – Height of the screen window (in pixels). If None, window_height equals to 75% of the display height.
Plotting Configuration
- class curvipy.PlottingConfiguration(plotting_speed: int = 10, curve_color: str = '#457B9D', curve_width: int = 4, vector_color: str = '#E63946', vector_width: int = 3, vector_head_size: int = 10)
Defines the configuration for plotting curves and vectors.
- Parameters
plotting_speed (int) – Curve plotting speed. Integer from 1 to 10. Defaults to 10.
curve_color (str) – Curve color. Can either be a name or a hex color code. Defaults to “#457B9D”.
curve_width (int) – Curve width. Defaults to 4.
vector_color (str) – Vector color. Can either be a name or a hex color code. Defaults to “#E63946”.
vector_width (int) – Vector width. Defaults to 3.
vector_head_size (int) – Size of vectors’ head. Defaults to 10.
Axes Configuration
- class curvipy.AxesConfiguration(show_axes: bool = True, show_axes_direction: bool = False, axes_color: str = '#70CBCE', axes_width: int = 2, ticks_font: tuple[str, str, str] = ('Verdana', 8, 'normal'), ticks_color: str = '#000000', x_ticks: int = 10, x_ticks_distance: int = 1, x_ticks_decimals: int = 2, x_ticks_align: str = 'down', y_ticks: int = 10, y_ticks_distance: int = 1, y_ticks_decimals: int = 2, y_ticks_align: str = 'left')
Defines the configuration for the plotter axes.
- Parameters
show_axes (bool) – If True, x-axis and y-axis are shown. Defaults to True.
show_axes_direction (bool) – If True, Plotter replaces the last tick of both axes with an arrow that indicates the axis direction. Defaults to False.
axes_color (str) – Axis color. Can either be a name or a hex color code. Defaults to “#70CBCE”.
axes_width (int) – Axis width. Defaults to 2.
ticks_font (tuple[str, str, str]) – A triple (fontname, fontsize, fonttype). Defaults to (“Verdana”, 8, “normal”).
ticks_color (str) – Ticks text color. Can either be a name or a hex color code. Defaults to “#000000”.
x_ticks (int) – Number of ticks of half the x-axis, i.e. total x-axis ticks equals 2 * x_ticks. Defaults to 10.
x_ticks_distance (int) – Distance between each x-axis tick. E.g. if x_ticks_distance equals 1, x-axis ticks will be {…, -2, -1, 0, 1, 2, …}. Defaults to 1.
x_ticks_decimals (int) – Number of decimals of the x-axis ticks number. Defaults to 2.
x_ticks_align (str) – Can either be “top” or “down”. Defines if the x-axis ticks number will be placed upside or downside the x-axis. Defaults to “down”.
x_ticks – Number of ticks of half the y-axis, i.e. total y-axis ticks equals 2 * y_ticks. Defaults to 10.
y_ticks_distance (int) – Distance between each y-axis tick. E.g. if y_ticks_distance equals 0.5, y-axis ticks will be {…, -1, -0.5, 0, 0.5, 1, …}. Defaults to 1.
y_tick_decimals (int) – Number of decimals of the y-axis ticks number. Defaults to 2.
y_ticks_align (str) – Can either be “left” or “right”. Defines if the y-axis ticks number will be placed to the left or to the right of the y-axis. Defaults to “left”.
Curves
With Curvipy you can plot two-dimensional curves. In this section you can find classes provided by Curvipy for defining curves.
Curve
- class curvipy.Curve
Base class for all two-dimensional curves.
- abstract points() list[Union[int, float]]
Returns a sorted list of curve point. The position of the points indicates the order in which they will be plotted.
- Returns
A list of curve points. The position of the points indicates the order in which they will be plotted.
- Return type
list[int or float]
Example:
import math
class Sin(Curve):
def __init__(self, amplitude, frequency):
self.interval = curvipy.Interval(-2 * math.pi, 2 * math.pi, 100)
self.function = lambda x: amplitude * math.sin(x * frequency)
def points(self):
return [(x, self.function(x)) for x in self.interval]
You can define your own Curve class or use on the classes provided by Curvipy shown below.
Interval
- class curvipy.Interval(start: Union[int, float], end: Union[int, float], samples: int)
Interval in which a curve will be plotted.
The interval is splitted into a list of values in which the curve will be evaluated. These values are defined by the number of samples specified. The more samples, the more precise the curve plot is.
- Parameters
start (int or float) – Real number in which the interval starts.
end (int or float) – Real number in which the interval ends.
samples (int) – Number of values within the interval. The more samples, the more precise the curve plot is.
Example: Suppose we want to plot the function √x with 20 samples.
import curvipy
valid_interval = curvipy.Interval(start=0, end=10, samples=20)
# All numbers from start to end belong to √x domain.
invalid_interval = curvipy.Interval(start=-10, end=0, samples=20)
# Negative numbers don't belong to √x domain.
Function
- class curvipy.Function(function: Callable[[Union[int, float]], Union[int, float]], interval: Interval)
Function that given a real number returns another real number y = f(x).
- Parameters
function (Callable[[int or float], int or float]) – Function that given an integer or float returns another integer or float.
interval (Interval) – The interval from which the curve will be plotted.
- points() list[Union[int, float]]
Returns the function point for each value in the given interval.
- Returns
A list of function points.
- Return type
list[int or float]
Example:
import math
import curvipy
interval = curvipy.Interval(-2 * math.pi, 2 * math.pi, 200)
curve = curvipy.Function(math.sin, interval)
Parametric Function
- class curvipy.ParametricFunction(parametric_function: Callable[[Union[int, float]], tuple[Union[int, float], Union[int, float]]], interval: Interval)
Function that given a real number returns a 2-dimensional vector f(t) = <x(t), y(t)>.
- Parameters
function (Callable[[int or float], tuple[int or float, int or float]]) – Function that given an integer or float returns a tuple containing two integers or floats.
interval (Interval) – The interval from which the parametric function will be plotted.
- points() list[Union[int, float]]
Returns the parametric function point for each value in the given interval.
- Returns
A list of parametric function points.
- Return type
list[int or float]
Example:
import curvipy
interval = curvipy.Interval(-5, 5, 200)
parametric_function = lambda t: (t, t**2)
curve = curvipy.ParametricFunction(parametric_function, interval)
Transformed Curve
- class curvipy.TransformedCurve(matrix: tuple[tuple[Union[int, float], Union[int, float]], tuple[Union[int, float], Union[int, float]]], curve: Curve)
Applies a linear transformation (defined as a 2x2 matrix) to the given curve.
- Parameters
matrix (tuple[ tuple[int or float, int or float], tuple[int or float, int or float] ]) – Linear transformation represented as a 2x2 matrix.
curve (Curve) – Curve to be transformed.
- points() list[Union[int, float]]
Applies a linear transformation to each point of the curve.
- Returns
A list of transformed curve points.
- Return type
list[int or float]
How it works:
Given a linear transformation \(T\) with its respective transformation matrix \(M_{2 \times 2}\) and a set of points \(P\) from a curve \(C\), TransformedCurve.points() computes the set of points
which defines the transformed curve \(T(C)\).
Example:
import math
import curvipy
interval = curvipy.Interval(-2 * math.pi, 2 * math.pi, 200)
curve = curvipy.Function(math.sin, interval)
rotation_matrix = ((0, 1), (-1, 0)) # 90° anticlockwise rotation
rotated_curve = curvipy.TransformedCurve(curve, rotation_matrix) # Rotated sin(x)
Vectors
- class curvipy.Vector(head: tuple[Union[int, float], Union[int, float]], tail: tuple[Union[int, float], Union[int, float]] = (0, 0))
Two-dimensional vector
\[\vec{v} = (v_1, v_2)\]- head
Two-dimensional point at which the vector is pointing.
- Type
tuple[int or float, int or float]
- tail
Two-dimensional point at which the vector starts.
- Type
tuple[int or float, int or float]
- property components: tuple[Union[int, float], Union[int, float]]
Vector head point when tail is moved to the origin.
- property norm: float
Norm of the vector.
- property angle: float
Angle between the vector and the x-axis in radians. This angle is a real number in the interval [-π, π]. If the angle is positive it indicates an anticlockwise rotation from the x-axis, this means that the vector is on the first or the second quadrant. If the angle is negative it indicates a clockwise rotation from the x-axis, this means that the vector is on the third or fourth quadrant.
- place(point: tuple[Union[int, float], Union[int, float]]) None
Moves the vector to the given point, that is, the vector tail is placed on the specified coordinates.
- Parameters
point (tuple[int or float, int or float]) – Two-dimensional point to which the vector is moved.
Example
v = curvipy.Vector(tail=(-1, -1), head=(2, 2)) v.place((-2, 2)) v.tail >>> (-2, 2) v.head >>> (1, 5)
- __getitem__(i: int) Union[int, float]
Returns the component at index i.
Example
v = curvipy.Vector(tail=[-1, -1], head=[2, 3]) v.components >>> (3, 4) v[0] >>> 3 v[1] >>> 4
- __mul__(scalar: Union[int, float]) Vector
Defines vector scaling. The scaled vector preserves the vector tail.
Example
v = curvipy.Vector(tail=[-1, -1], head=[2, 2]) w = v * 2 w.tail >>> (-1, -1) w.head >>> (5, 5) w.components >>> (6, 6)
- __add__(vector: Vector) Vector
Defines vector addition. Result vector is placed at the origin, that is, the tail of the result vector is (0, 0).
Example
v = curvipy.Vector(tail=[-1, -1], head=[2, 2]) w = curvipy.Vector(tail=[2, 0], head=[1, -2]) n = v + w n.tail >>> (0, 0) n.head >>> (2, 1)