Source code for sectionproperties.post.post

import numpy as np
import matplotlib.pyplot as plt


[docs]def setup_plot(ax, pause): """Executes code required to set up a matplotlib figure. :param ax: Axes object on which to plot :type ax: :class:`matplotlib.axes.Axes` :param bool pause: If set to true, the figure pauses the script until the window is closed. If set to false, the script continues immediately after the window is rendered. """ if not pause: plt.ion() plt.show() else: plt.ioff()
[docs]def finish_plot(ax, pause, title=''): """Executes code required to finish a matplotlib figure. :param ax: Axes object on which to plot :type ax: :class:`matplotlib.axes.Axes` :param bool pause: If set to true, the figure pauses the script until the window is closed. If set to false, the script continues immediately after the window is rendered. :param string title: Plot title """ ax.set_title(title) ax.set_aspect('equal', anchor='C') plt.tight_layout() if pause: plt.show() else: plt.draw() plt.pause(0.001)
[docs]def draw_principal_axis(ax, phi, cx, cy): """ Draws the principal axis on a plot. :param ax: Axes object on which to plot :type ax: :class:`matplotlib.axes.Axes` :param float phi: Principal axis angle in radians :param float cx: x-location of the centroid :param float cy: y-location of the centroid """ # get current axis limits (xmin, xmax) = ax.get_xlim() (ymin, ymax) = ax.get_ylim() lims = [xmin, xmax, ymin, ymax] # form rotation matrix R = np.array([[np.cos(phi), -np.sin(phi)], [np.sin(phi), np.cos(phi)]]) # get basis vectors in the directions of the principal axes x11_basis = R.dot(np.array([1, 0])) y22_basis = R.dot(np.array([0, 1])) def add_point(vec, basis, centroid, num, denom): """Adds a point to the list *vec* if there is an intersection.""" if denom != 0: point = basis * num / denom + centroid vec.append([point[0], point[1]]) def get_prinicipal_points(basis, lims, centroid): """Determines the intersections of the prinicpal axis with the four lines defining a bounding box around the limits of the cross-section. The middle two intersection points are returned for plotting. :param basis: Basis (unit) vector in the direction of the principal axis :type basis: :class:`numpy.ndarray` :param lims: Tuple containing the axis limits *(xmin, xmax, ymin, ymax)* :type lims: tuple(float, float, float, float) :param centroid: Centroid *(cx, cy)* of the cross-section, through which the principal axis passes :type centroid: list[float, float] """ pts = [] # initialise list containing the intersection points # add intersection points to the list add_point(pts, basis, centroid, lims[0] - centroid[0], basis[0]) add_point(pts, basis, centroid, lims[1] - centroid[0], basis[0]) add_point(pts, basis, centroid, lims[2] - centroid[1], basis[1]) add_point(pts, basis, centroid, lims[3] - centroid[1], basis[1]) # sort point vector pts = np.array(pts) pts = pts[pts[:, 0].argsort()] # stackoverflow sort numpy array by col # if there are four points, take the middle two points if len(pts) == 4: return pts[1:3, :] return pts # get intersection points for the 11 and 22 axes x11 = get_prinicipal_points(x11_basis, lims, [cx, cy]) y22 = get_prinicipal_points(y22_basis, lims, [cx, cy]) # plot the principal axis ax.plot(x11[:, 0], x11[:, 1], 'k--', alpha=0.5, label='11-axis') ax.plot(y22[:, 0], y22[:, 1], 'k-.', alpha=0.5, label='22-axis')