Skip to main content

Plot Draw

This library is designed to provide a set of functions for drawing various types of plots, arrows, segments, and shapes using Matplotlib. These functions allow for customized plotting and annotation of graphical elements.

General

blank_canvas

[Source]

blank_canvas(
width = 800,
height = 600,
color = "white"
)

Description:

Creates a blank image with specified width and height, displaying a grid.

Arguments:

ArgumentTypeDescription
widthint (optional)The width of the image in pixels. (Default is 800)
heightint (optional)The height of the image in pixels. (Default is 600)
colorstr(optional)The background color of the image. (Default is 'white')

Returns:

Return TypeDescription
plt.AxesThe Axes object of the created blank image.

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
ax = plot_draw.blank_canvas()
plot = msc.print_plot(plt)
return {'plot': plot}

white canvas

lines and arrows

draw_line

[Source]

draw_line(
start,
end,
color = "black",
line_width = None,
ax = None
)

Description:

Draws a segment between two points with a specified line width and color.

Arguments:

ArgumentTypeDescription
starttupleThe coordinates of the starting point (x, y).
endtupleThe coordinates of the final point (x, y).
colorstr (optional)The color of the segment. (Default is 'black')
line_widthfloat (optional)The width of the segment. (Default is None)
axplt.Axes (optional)The Axes object to draw the segment on. (Default is None)

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
plot_draw.draw_line((0, 0), (1, 1), color='blue', line_width=0.005)
plot = msc.print_plot(plt)
return {'plot': plot}

segment

draw_arrow

[Source]

draw_arrow(
start,
end,
thickness = 5,
color = "black",
text = "",
text_offset = 0.1,
head_width = 0.08,
head_length = 0.08,
fontsize = 12,
ax = None
)

Description:

Draws an arrow between two points on a plot.

Arguments:

ArgumentTypeDescription
startTuple[float, float]The starting point of the arrow (x, y).
endTuple[float, float]The ending point of the arrow (x, y).
thicknessfloat (optional)The thickness of the arrow line. (Default is 5)
colorstr (optional)The color of the arrow. (Default is 'black')
textstr (optional)Text to display near the arrow. (Default is None)
text_offsetfloat (optional)Distance from the arrow end point where the text will be placed. (Default is 0.5)
head_widthfloat (optional)Width of the arrow head. (Default is 0.08)
head_lengthfloat (optional)Length of the arrow head. (Default is 0.08)
fontsizefloat (optional)Font size of the text. (Default is 12)
axplt.Axes (optional)The Axes object to draw the arrow on. (Default is None)

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
plot_draw.draw_arrow((0, 0), (1, 1), color='red', text='Arrow')
plt.xlim(-1, 2)
plt.ylim(-1, 2)
plot = msc.print_plot(plt)
return {'plot': plot}

Output:

Arrow

draw_double_arrowhead

[Source]

draw_double_arrowhead(
start,
end,
color = "black",
line_thickness = 1,
ax = None
)

Description:

Draws a double arrowhead between two points.

Arguments:

ArgumentTypeDescription
starttupleCoordinates of the start point (x, y).
endtupleCoordinates of the end point (x, y).
colorstr (optional)Color of the arrow and line. (Default is 'black')
line_thicknessfloat (optional)Thickness of the line. (Default is 1)
axplt.Axes (optional)The Axes object to draw the arrow on.

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
plot_draw.draw_double_arrowhead(start=(0, 0), end=(1, 1))
plot = msc.print_plot(plt)
return {'plot': plot}

double arrowhead

vertical_arrow_rain

[Source]

vertical_arrow_rain(
quantity,
start,
end,
y_origin = 0,
arrow_color = "blue",
head_width = 0.05,
head_length = 0.1,
ax = None
)

Description:

Draws a specific quantity of arrows from equidistant points on a segment that extends from start to end, with all arrows pointing to y_origin.

Arguments:

ArgumentTypeDescription
quantityintNumber of arrows to draw.
starttupleTuple (x, y) representing the starting point of the segment.
endtupleTuple (x, y) representing the final point of the segment.
y_originfloaty-coordinate to which all arrows should point. (Default is 0)
arrow_colorstrColor of the arrows. (Default is 'blue')
head_widthfloatWidth of the arrow head. (Default is 0.05)
head_lengthfloatLength of the arrow head. (Default is 0.1)
axplt.AxesThe Axes object to draw the arrows on.

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
plot_draw.vertical_arrow_rain(quantity=5, start=(0, 1), end=(1, 1), y_origin=0)
plot = msc.print_plot(plt)
return {'plot': plot}

vertical arrow rain

horizontal_arrow_rain

[Source]

horizontal_arrow_rain(
quantity,
start,
end,
x_origin = 0,
arrow_color = "blue",
head_width = 0.05,
head_length = 0.1,
ax = None
)

Description:

Draws a specific quantity of arrows from a vertical line at x_origin to equidistant points on a segment that extends from start to end.

Arguments:

ArgumentTypeDescription
quantityintNumber of arrows to draw.
starttupleTuple (x, y) representing the starting point of the segment.
endtupleTuple (x, y) representing the final point of the segment.
x_originfloatx-coordinate from which all arrows originate. (Default is 0)
arrow_colorstrColor of the arrows. (Default is 'blue')
head_widthfloatWidth of the arrow head. (Default is 0.05)
head_lengthfloatLength of the arrow head. (Default is 0.1)
axplt.AxesThe Axes object to draw the arrows on. (Default is None)

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
plot_draw.horizontal_arrow_rain(quantity=5, start=(1, 1), end=(1, 0), x_origin=0)
plot = msc.print_plot(plt)
return {'plot': plot}

horizontal arrow rain

Shapes

draw_circle

[Source]

draw_circle(
center = (0, 0),
radius = 10,
color = "black",
ax = None
)

Description:

Draws a custom circle on a given axis.

Arguments:

ArgumentTypeDescription
centertuple (optional)The center point of the circle (x, y). (Default is (0, 0))
radiusfloat (optional)The size of the circle. (Default is 100)
colorstr (optional)The color of the circle. (Default is 'black')
axplt.Axes (optional)The Axes object to draw the circle on. (Default is None)

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
plot_draw.draw_circle((100, 100), radius=20, color='red')
plot = msc.print_plot(plt)
return {'plot': plot}

# Expected output: A plot displaying a red circle with a center at (100, 100) and size 200.

arrow

draw_arc

[Source]

draw_arc(
radius,
start_angle,
end_angle,
center = (0, 0),
degrees = False,
color = "red",
text = "",
text_offset = 0.1,
fontsize = 12,
ax = None,
)

Description:

Draws an arc of a circumference with a given radius between two angles.

Arguments:

ArgumentTypeDescription
radiusfloatThe radius of the circumference.
start_anglefloatThe starting angle of the arc in radians.
end_anglefloatThe ending angle of the arc in radians.
centertuple (optional)The center of the circumference. (Default is (0, 0))
degreesbool (optional)Whether the angles are. (Default is False)
colorstr (optional)The color of the arc. (Default is 'red')
textstr (optional)Text to display near the arc. (Default is None)
text_offsetfloat (optional)Distance from the arc end point where the text will be placed. (Default is 0.1)
fontsizefloat (optional)Font size of the text. (Default is 12)
axplt.Axes (optional)The Axes object to draw the arc on. (Default is None)

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
plot_draw.draw_arc(5, 0, 90, degrees=True)
plot = msc.print_plot(plt)
return {'plot': plot}.

quarter circle arc

draw_rounded_rectangle

[Source]

draw_rounded_rectangle(
width,
height,
center = (0, 0),
corner_radius = 0.5,
color = "black",
ax = None
)

Description:

Draws a rounded rectangle with specified properties.

Arguments:

ArgumentTypeDescription
widthfloatThe width of the rounded rectangle.
heightfloatThe height of the rounded rectangle.
centertuple (optional)The middle point of the top side of the rounded rectangle (x, y). (Default is (0, 0))
corner_radiusfloat (optional)The radius of the corners. (Default is 0.5)
colorstr (optional)The color of the rectangle. (Default is 'black')
axplt.Axes (optional)The Axes object to draw the rectangle on. (Default is None)

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
plot_draw.draw_rounded_rectangle(4, 2, center = (0,0), corner_radius = 0.5, color='blue')
plot = msc.print_plot(plt)
return {'plot': plot}

rounded rectangle

Axes

draw_two_axes

[Source]

draw_two_axes(
arrow_length,
line_thickness = 1.5,
text_offset = 0.1,
negative_y = False,
negative_x = False,
ax = None
)

Description:

Draws two axes representing the x and y directions.

Arguments:

ArgumentTypeDescription
arrow_lengthfloatLength of the arrows representing the axes.
line_thicknessfloatThickness of the arrows representing the axes. (Default is 1.5)
text_offsetfloatOffset for the axis labels. (Default is 0.1)
negative_yboolDraws negative y-axis if True. (Default is False)
negative_xboolDraws negative x-axis if True. (Default is False)
axplt.AxesThe Axes object to draw the axes on.

Returns:

Return TypeDescription
plt.AxesAxes object.

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
ax = plot_draw.draw_two_axes(arrow_length=1.0, negative_y=True, negative_x=True)
plot = msc.print_plot(plt)
return {'plot': plot}

two axes

draw_two_inclined_axes

[Source]

draw_two_inclined_axes(
arrow_length,
arrow_thickness = 2,
text_offset = 0.1,
negative_y = False,
negative_x = False,
ax = None
)

Description:

Draws two inclined axes (x and y) with optional negative directions.

Arguments:

ArgumentTypeDescription
arrow_lengthfloatThe length of the arrows representing the axes.
arrow_thicknessfloat (optional)The thickness of the arrows. (Default is 2)
text_offsetfloat (optional)The distance between the end of the arrow and the label text. (Default is 0.1)
negative_ybool (optional)Draws negative y-axis if True. (Default is False)
negative_xbool (optional)Draws negative x-axis if True. (Default is False)
axplt.Axes (optional)The Axes object to draw the axes on. (Default is None)

Returns:

Return TypeDescription
plt.AxesThe Axes object with the drawn axes.

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
ax = plot_draw.draw_two_inclined_axes(arrow_length=1, negative_y=True, negative_x=True)
plot = msc.print_plot(plt)
return {'plot': plot}

2 inclined axes

draw_three_axes

[Source]

draw_three_axes(
arrow_length,
arrow_thickness = 2,
text_offset = 0.1,
negative_y = False,
negative_x = False,
ax = None
)

Description:

Draws a set of three axes (x, y, z) with optional negative directions for x and y.

Arguments:

ArgumentTypeDescription
arrow_lengthfloatThe length of the arrows representing the axes.
arrow_thicknessfloat (optional)The thickness of the arrows. (Default is 2)
text_offsetfloat (optional)The distance between the end of the arrow and the label text. (Default is 0.1)
negative_ybool (optional)Draws negative y-axis if True. (Default is False)
negative_xbool (optional)draws negative x-axis if True. (Default is False)
axplt.Axes (optional)The Axes object to draw the axes on. (Default is None)

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecimcalc.plot_draw as plot_draw

def main(inputs):
plot_draw.draw_three_axes(arrow_length=1, negative_y=True, negative_x=True)
plot = msc.print_plot(plt)
return {'plot': plot}

three axes

draw_three_axes_rotated

[Source]

draw_three_axes_rotated(
arrow_length,
line_thickness = 1.5,
text_offset = 0.2,
negative_y = False,
negative_x = False,
ax = None
)

Description:

Draws three rotated axes in a 3D coordinate system.

Arguments:

ArgumentTypeDescription
arrow_lengthfloatThe length of the arrow.
line_thicknessfloatThe thickness of the line.
text_offsetfloatThe offset of the text from the arrow.
negative_yboolWhether to include negative y-axis (Default is False).
negative_xboolWhether to include negative x-axis (Default is False).

Returns:

Return TypeDescription
plt.AxesThe matplotlib Axes object containing the plot.

Example:

import matplotlib.pyplot as plt
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
ax = plot_draw.draw_three_axes_rotated(arrow_length=1.0, negative_y=True, negative_x=True)
plot = msc.print_plot(plt)
return {'plot': plot}

three axes rotated

Calculations

calculate_midpoint

[Source]

calculate_midpoint(
coord1,
coord2
)

Description:

Calculates the midpoint between two coordinates.

Arguments:

ArgumentTypeDescription
coord1Tuple[float, float]The first coordinate (x, y).
coord2Tuple[float, float]The second coordinate (x, y).

Returns:

Return TypeDescription
(float, float)A tuple containing the coordinates of the midpoint.

Example:

import mecsimcalc.plot_draw as plot_draw

def main(inputs):
midpoint = plot_draw.calculate_midpoint((0, 0), (2, 2))
return {"midpoint": midpoint}

# Expected output: {"midpoint": (1.0, 1.0)}

calculate_intersection_point

[Source]

calculate_intersection_point(
point1,
angle1,
point2,
angle2,
degrees = False
)

Description:

Calculates the intersection point of two lines defined by points and angles.

Arguments:

ArgumentTypeDescription
point1tupleThe coordinates of the first point (x, y) through which the first line passes.
angle1floatThe angle of the first line.
point2tupleThe coordinates of the second point (x, y) through which the second line passes.
angle2floatThe angle of the second line.
degreesbool (optional)Whether the angles are. (Default is False)

Returns:

Return TypeDescription
(float, float)The coordinates of the intersection point (x, y).

Example:

import mecsimcalc.plot_draw as plot_draw

def main(inputs):
intersection = plot_draw.calculate_intersection_point((0, 0), 45, (1, 1), 135, degrees=True)
return {"intersection"ersection}

# Expected output: {"intersection": (1.0, 0.9999999999999999)}

calculate_arrow_endpoint

[Source]

calculate_arrow_endpoint(
start,
angle,
length,
degrees = False
)

Description:

Calculates the end point of an arrow in pixel coordinates.

Arguments:

ArgumentTypeDescription
starttupleThe starting point of the arrow (x, y) in pixel coordinates.
anglefloatThe angle of the arrow.
lengthfloatThe length of the arrow.
degreesboolWhether the angle is. (Default is False)

Returns:

Return TypeDescription
(float, float)The end point of the arrow (x, y) in pixel coordinates.

Example:

import mecsimcalc.plot_draw as plot_draw

def main(inputs):
endpoint = plot_draw.calculate_arrow_endpoint((100, 200), 45, 50, degrees=True)
return {"endpoint": endpoint}

# Expected output: {"endpoint": (135.35533905932738, 235.35533905932738)}

calculate_angle

[Source]

calculate_angle(
start: tuple,
end: tuple,
degrees: bool = False
)

Description:

Calculates the angle (in degrees) between two points.

Arguments:

ArgumentTypeDescription
starttupleTuple (x, y) representing the starting point.
endtupleTuple (x, y) representing the final point.
degreesboolWhether to return the angle in degrees. (Default is False)

Returns:

Return TypeDescription
floatThe angle between the two points.

Example:

import mecsimcalc.plot_draw as plot_draw

def main(inputs):
angle = plot_draw.calculate_angle(start=(0, 0), end=(1, 1), degrees=True)
return {"angle": angle}

# Expected output: {"angle": 45.0}

get_arc_points

[Source]

get_arc_points(
start_angle,
end_angle,
radius,
center: Union[tuple, list]
)

Description:

Calculates points along a circular arc defined by a start angle and an end angle.

Arguments:

ArgumentTypeDescription
start_anglefloatThe starting angle of the arc.
end_anglefloatThe ending angle of the arc.
radiusfloatThe radius of the arc.
centerTuple[float, float]The coordinates of the center of the arc [cx, cy].

Returns:

Return TypeDescription
Tuple[np.ndarray, np.ndarray]The x and y coordinates of the arc points.

Example:

import matplotlib.pyplot as plt
import numpy as np
import mecsimcalc as msc
import mecsimcalc.plot_draw as plot_draw

def main(inputs):
arc_points_x1, arc_points_y1 = plot_draw.get_arc_points(90, 240, 0.25, (0, -0.25), degrees=True)
plt.plot(arc_points_x1, arc_points_y1, 'k')
plot = msc.print_plot(plt)
return {'plot': plot}

arc points