Constant plotpy::PYTHON_HEADER
source · pub const PYTHON_HEADER: &str = "### file generated by the 'plotpy' Rust crate
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tck
import matplotlib.patches as pat
import matplotlib.path as pth
import matplotlib.patheffects as pff
import matplotlib.lines as lns
import matplotlib.transforms as tra
import mpl_toolkits.mplot3d
# Variable to handle NaN values coming from Rust
NaN = np.nan
# List of additional objects to calculate bounding boxes
EXTRA_ARTISTS = []
# Adds an entity to the EXTRA_ARTISTS list to calculate bounding boxes
def add_to_ea(obj):
global EXTRA_ARTISTS
if obj!=None: EXTRA_ARTISTS.append(obj)
# Is a dictionary of mplot3d objects (one for each subplot_3d)
THREE_D = dict()
# Is a tuple holding the key to the current THREE_D object (defines the subplot_3d)
THREE_D_ACTIVE = (1,1,1)
# Creates or returns the mplot3d object with the current subplot_3d definition specified by THREE_D_ACTIVE
def ax3d():
global THREE_D
global THREE_D_ACTIVE
if not THREE_D_ACTIVE in THREE_D:
a, b, c = THREE_D_ACTIVE
THREE_D[THREE_D_ACTIVE] = plt.gcf().add_subplot(a,b,c,projection='3d')
THREE_D[THREE_D_ACTIVE].set_xlabel('x')
THREE_D[THREE_D_ACTIVE].set_ylabel('y')
THREE_D[THREE_D_ACTIVE].set_zlabel('z')
add_to_ea(THREE_D[THREE_D_ACTIVE])
return THREE_D[THREE_D_ACTIVE]
# Specifies the THREE_D_ACTIVE parameters to define a subplot_3d
def subplot_3d(a,b,c):
global THREE_D_ACTIVE
THREE_D_ACTIVE = (a,b,c)
ax3d()
# Transforms data limits to axis limits
def data_to_axis(coords):
plt.axis() # must call this first
return plt.gca().transLimits.transform(coords)
# Transforms axis limits to data limits
def axis_to_data(coords):
plt.axis() # must call this first
return plt.gca().transLimits.inverted().transform(coords)
# Configures the aspect of axes with a same scaling from data to plot units for x, y and z.
def set_equal_axes():
global THREE_D
if len(THREE_D) == 0:
ax = plt.gca()
ax.axes.set_aspect('equal')
return
try:
ax = ax3d()
ax.set_box_aspect([1,1,1])
limits = np.array([ax.get_xlim3d(), ax.get_ylim3d(), ax.get_zlim3d()])
origin = np.mean(limits, axis=1)
radius = 0.5 * np.max(np.abs(limits[:, 1] - limits[:, 0]))
x, y, z = origin
ax.set_xlim3d([x - radius, x + radius])
ax.set_ylim3d([y - radius, y + radius])
ax.set_zlim3d([z - radius, z + radius])
except:
import matplotlib
print('VERSION of MATPLOTLIB = {}'.format(matplotlib.__version__))
print('ERROR: set_box_aspect is missing in this version of Matplotlib')
################## plotting commands follow after this line ############################
";
Expand description
Commands to be added at the beginning of the Python script
The python definitions are:
NaN
– Variable to handle NaN values coming from RustEXTRA_ARTISTS
– List of additional objects that must not be ignored when saving the figureadd_to_ea
– Adds an entity to the EXTRA_ARTISTS list to prevent them being ignored when Matplotlib decides to calculate the bounding boxes. The Legend is an example of entity that could be ignored by the savefig command (this is issue is prevented here).THREE_D
– Is a dictionary of mplot3d objects (one for each subplot_3d)THREE_D_ACTIVE
– Is a tuple holding the key to the current THREE_D object (defines the subplot_3d)ax3d
– Creates or returns the mplot3d object with the current subplot_3d definition specified by THREE_D_ACTIVEsubplot_3d
– Specifies the THREE_D_ACTIVE parameters to define a subplot_3ddata_to_axis
– Transforms data limits to axis limitsaxis_to_data
– Transforms axis limits to data limitsset_equal_axes
– Configures the aspect of axes with a same scaling from data to plot units for x, y and z. For example a circle will show as a circle in the screen and not an ellipse. This function also handles the 3D case which is a little tricky with Matplotlib. In this case (3D), the version of Matplotlib must be greater than 3.3.0.