boviz package
Submodules
boviz.cli module
boviz.config module
boviz.curves module
- boviz.curves.plot_curves(data: list[tuple[ndarray, ndarray]], label: list[str], information: str | None = None, factor: list[tuple[tuple, tuple]] | None = None, time_step: list[int | list | tuple] | None = None, xy_label: tuple[str, str] | None = None, use_marker: list[bool] | None = None, use_scatter: list[bool] | None = None, line_style: list[str] | None = None, tick_interval_x: float | None = None, tick_interval_y: float | None = None, legend_location: str | None = None, xlim: tuple[float, float] | None = None, ylim: tuple[float, float] | None = None, highlight_x: float | None = None, split_legend: bool = False, show_legend: bool = True, show_residual: bool = False, show_title: bool = True, title_figure: str | None = None, legend_ncol: int | None = None, ylog: bool = False, sci: tuple[float, float] = (None, None), color_group: list[int] | None = None, show: bool = False, save: bool = False, font_style: str | None = None, font_weight: str = 'bold', figure_format: str = 'png') str[源代码]
直接绘制内存中的数据(NumPy 数组)。参数含义同 plot_curves_csv。
- boviz.curves.plot_curves_csv(path: list[str], label: list[str], x: list[int], y: list[int], information: str | None = None, factor: list[tuple[tuple, tuple]] | None = None, time_step: list[int | list | tuple] | None = None, xy_label: tuple[str, str] | None = None, use_marker: list[bool] | None = None, use_scatter: list[bool] | None = None, line_style: list[str] | None = None, tick_interval_x: float | None = None, tick_interval_y: float | None = None, legend_location: str | None = None, xlim: tuple[float, float] | None = None, ylim: tuple[float, float] | None = None, highlight_x: float | None = None, split_legend: bool = False, show_residual: bool = False, show_legend: bool = True, show_title: bool = True, title_figure: str | None = None, legend_ncol: int | None = None, legend_fontsize: int | float | str | None = None, ylog: bool = False, sci: tuple[float, float] = [None, None], color_group: list[int] | None = None, show: bool = False, save: bool = False, font_style: str | None = None, font_weight: str = 'bold', figure_format: str = 'png') str[源代码]
从 CSV 文件读取数据并绘制科学曲线。支持多曲线对比、样式定制及残差分析。
- 参数:
path (list[str]) -- CSV 文件路径列表。
label (list[str]) -- 每条曲线的图例标签。
x (list[int]) -- X 轴数据所在的列索引(从0开始)。
y (list[int]) -- Y 轴数据所在的列索引(从0开始)。
information (str, optional) -- 附加信息,用于生成文件名的后缀。默认为 None。
factor (list[tuple[tuple, tuple]], optional) -- 数据缩放与平移因子。 格式为 [((x_scale, x_offset), (y_scale, y_offset)), ...]。 默认为 (1.0, 0.0)。
time_step (list[int | list | tuple], optional) -- 数据截取设置。 - 整数 N: 取前 N 行。 - 列表 [start, end]: 取 start 到 end 行。
xy_label (tuple[str, str], optional) -- (X轴名称, Y轴名称)。若不提供,将尝试使用 CSV 列名。
use_marker (list[bool], optional) -- 是否为每条曲线启用标记点样式。默认为 False。
use_scatter (list[bool], optional) -- 是否以散点图形式绘制。默认为 False。
line_style (list[str], optional) -- 指定每条曲线的线型(如 '-', '--')。默认为 '-'。
tick_interval_x (float, optional) -- 强制 X 轴主刻度间隔。
tick_interval_y (float, optional) -- 强制 Y 轴主刻度间隔。
legend_location (str, optional) -- 图例位置,如 'upper right', 'best'。
xlim (tuple[float, float], optional) -- X 轴显示范围 (min, max)。
ylim (tuple[float, float], optional) -- Y 轴显示范围 (min, max)。
split_legend (bool, optional) -- 是否将图例单独保存为一张图片。默认为 False。
show_residual (bool, optional) -- 是否绘制残差子图(相对于第一条曲线)。默认为 False。
show_legend (bool, optional) -- 是否显示图例。默认为 True。
show_title (bool, optional) -- 是否显示标题。默认为 True。
title_figure (str, optional) -- 图像标题,同时用于文件命名。
legend_ncol (int, optional) -- 图例列数。
ylog (bool, optional) -- Y 轴是否使用对数坐标。
sci (tuple[float, float], optional) -- 科学计数法缩放因子 [x_scale, y_scale]。
color_group (list[int], optional) -- 指定颜色分组索引,强制多条曲线使用相同颜色。
show (bool, optional) -- 是否在窗口显示图像。默认为 False。
save (bool, optional) -- 是否保存图像到文件。默认为 False。
font_style (str, optional) -- 字体风格 ('times', 'sans' 或 None)。
font_weight (str, optional) -- 字体粗细 ('bold' 或 'normal')。
figure_format (str, optional) -- 保存格式 ('png', 'pdf', 'svg')。默认为 'png'。
- 返回:
保存的图像文件路径。
- 返回类型:
str
- boviz.curves.plot_dual_curves_csv(path: list[str], label: list[str], x: list[int], y: list[int], path_right: list[str], label_right: list[str], x_right: list[int], y_right: list[int], information: str | None = None, factor: list[tuple[tuple, tuple]] | None = None, factor_right: list[tuple[tuple, tuple]] | None = None, time_step: list[int | list | tuple] | None = None, time_step_right: list[int | list | tuple] | None = None, xy_label: tuple[str, str] | None = None, y_label_right: str | None = None, use_marker: list[bool] | None = None, use_marker_right: list[bool] | None = None, use_scatter: list[bool] | None = None, use_scatter_right: list[bool] | None = None, tick_interval_x: float | None = None, tick_interval_y: float | None = None, tick_interval_y_right: float | None = None, legend_location: str | None = None, xlim: tuple[float, float] | None = None, ylim: tuple[float, float] | None = None, ylim_right: tuple[float, float] | None = None, split_legend: bool = False, show_legend: bool = False, match_axis_color: bool = True, add_axis_arrow: bool = False, show_title: bool = True, title_figure: str | None = None, legend_ncol: int | None = None, auto_sci: bool = True, sci: list[float] = [None, None, None], color_group: list[int] | None = None, color_group_right: list[int] | None = None, show: bool = False, save: bool = False, font_style: str | None = None, font_weight: str = 'bold', figure_format: str = 'png') str[源代码]
双轴绘图(风格统一版):线宽为1,实线,完美复刻 plot_curves_csv 风格。
boviz.heatmap module
- boviz.heatmap.plot_heatmap_exodus2d(path: str, variable: str, colorbar_range: tuple | None = None, time_step: int = 0, cmap: str = 'coolwarm', title_figure: str | None = None, show: bool = False, save: bool = False, information: str | None = None, font_style: str | None = None, font_weight: str = 'bold', show_ticks: bool = True)[源代码]
绘制 Exodus 2D 数据的热图。
- 参数:
path (str) -- Exodus 2D NetCDF 文件路径。
variable (str) -- 需要绘制的变量名。
colorbar_range (tuple, optional) -- 色条的取值范围 (vmin, vmax)。若为 None,则自动根据数据计算。
time_step (int, optional) -- 变量的时间步索引,默认为 0。
cmap (str, optional) -- 热图使用的颜色映射,默认为 'coolwarm', 可选值包括 'viridis', 'plasma', 'inferno', 'magma', 'cividis' 等。
title_figure (str, optional) -- 图像标题,默认为文件内自带标题。
show (bool, optional) -- 是否显示图像,默认为 False。
save (bool, optional) -- 是否保存图像,默认为 False。
information (str, optional) -- 附加信息,用于生成文件名后缀。
font_style (str, optional) -- 字体样式,默认为 Times。可选值为 'sans' 或 None。
font_weight (str, optional) -- 字体粗细,默认为 None。可选值为 'bold' 或 None。
show_ticks (bool, optional) -- 是否显示坐标轴刻度以及标题,默认为 True。
- 返回:
保存的图像路径。
- 返回类型:
str
- boviz.heatmap.plot_heatmap_exodus2d_grid(nx: int, ny: int, paths, variables, time_steps=0, cmap='coolwarm', colorbar_range: tuple | None = None, titles: list | None = None, suptitle: str | None = None, panel_title_size: int = 20, suptitle_size: int = 24, cbar_width: float = 0.025, cbar_pad: float = 0.025, information: str | None = None, font_style: str | None = None, font_weight: str = 'bold', show: bool = False, save: bool = False)[源代码]
绘制 Exodus2D 网格数据的热力图(支持多子图排版)。
- 参数:
nx (int) -- 水平子图数量。
ny (int) -- 垂直子图数量。
paths (list[str] | str) -- Exodus2D NetCDF 文件路径列表或单一路径(会广播)。
variables (list[str] | str) -- 变量名列表或单一变量名(会广播)。
time_steps (list[int] | int, optional) -- 时间步索引列表或单一索引(会广播),默认为 0。
cmap (str | list[str], optional) -- 颜色映射名称或列表,默认为 'coolwarm'(会广播)。
colorbar_range (tuple, optional) -- 色条取值范围 (vmin, vmax),默认为 None(自动计算)。
titles (list[str], optional) -- 每个子图的标题列表,默认为 None(使用文件内标题)。
suptitle (str, optional) -- 总标题,默认为 None。
panel_title_size (int, optional) -- 小图标题字号,默认为 20。
suptitle_size (int, optional) -- 总标题字号,默认为 24。
cbar_width (float, optional) -- 色条宽度(相对图像坐标),默认为 0.025。
cbar_pad (float, optional) -- 色条与右侧子图间距(相对图像坐标),默认为 0.025。
information (str, optional) -- 附加信息,用于生成文件名后缀。
font_style (str, optional) -- 字体样式,默认为 None。可选值为 'sans' 或 None。
font_weight (str, optional) -- 字体粗细,默认为 'bold'。可选值为 'bold' 或 'normal'.
show (bool, optional) -- 是否显示图像,默认为 False。
save (bool, optional) -- 是否保存图像,默认为 False。
- 返回:
保存的图像路径。
- 返回类型:
str
- boviz.heatmap.plot_heatmap_particle(particle_x_num: int, particle_y_num: int, particle_radius: float, border: float | None = None, cmap: str = 'coolwarm', title_figure: str = 'Initial Particle Schematic', show: bool = False, save: bool = False, information: str | None = None, surface_thickness: float = 3.0, tanh_offset: float = 0.05, font_style: str | None = None, font_weight: str = 'bold', show_ticks: bool = True)[源代码]
绘制初始粒子分布的热图。
- 参数:
particle_x_num (int) -- 粒子在x方向的数量。
particle_y_num (int) -- 粒子在y方向的数量。
particle_radius (float) -- 粒子的半径。
border (float, optional) -- 粒子布局的边界宽度,默认为 None。
cmap (str, optional) -- 热图使用的颜色映射,默认为 'coolwarm', 可选值包括 'viridis', 'plasma', 'inferno', 'magma', 'cividis' 等。
title_figure (str, optional) -- 图像标题,默认为 "Initial Particle Schematic"。
show (bool, optional) -- 是否显示图像,默认为 True。
save (bool, optional) -- 是否保存图像,默认为 True。
information (str, optional) -- 附加信息,用于生成文件名后缀。
surface_thickness (float, optional) -- 表面厚度,用于生成相场,默认为 3.0。
tanh_offset (float, optional) -- 相场的偏移量,默认为 0.05。
font_style (str, optional) -- 字体样式,默认为 Times。可选值为 'sans' 或 None。
font_weight (str, optional) -- 字体粗细,默认为 None。可选值为 'bold' 或 None。
show_ticks (bool, optional) -- 是否显示坐标轴刻度以及标题,默认为 True。
- 返回:
保存的图像路径。
- 返回类型:
str
boviz.histogram module
- boviz.histogram.plot_histogram(label: str, x: list[float], y: list[float], xy_label: tuple[str, str], information: str | None = None, factor: list[float, float] | None = None, title: str = 'Histogram', ylog: bool = False, sci: tuple[float, float] = [None, None], save: bool = False, show: bool = False, font_style: str | None = None, font_weight: str = 'bold', categorical_x: bool = True, show_values: bool = True) str[源代码]
绘制柱状图(直方图)
- 参数:
x (list[int]) -- 核数列表
y (list[float]) -- 对应的运行时间列表
label (str, optional) -- 图例标签. Defaults to "Runtime".
xy_label (tuple[str, str], optional) -- x轴和y轴标签. Defaults to ("Number of Cores", "Runtime (s)").
title (str, optional) -- 图表标题. Defaults to "Multi-core Performance".
legend_location (str, optional) -- 图例位置. Defaults to "best".
ylog (bool, optional) -- 是否使用对数y轴. Defaults to False.
sci (tuple[float, float], optional) -- x轴和y轴的科学计数法阈值. Defaults to [None, None].
save (bool, optional) -- 是否保存图表. Defaults to True.
show (bool, optional) -- 是否显示图表. Defaults to True.
font_style (str, optional) -- 字体风格,"sans"表示无衬线字体,None表示默认字体. Defaults to None.
font_weight (str, optional) -- 字体粗细,"bold"表示粗体,"normal"表示正常字体. Defaults to "bold".
categorical_x (bool, optional) -- 是否将x轴作为类别处理(等间距). Defaults to True.
- 返回:
保存的图表路径
- 返回类型:
str
boviz.schematic module
- boviz.schematic.plot_initial_capsule_schematic(coordinates: list, radii: list, lengths: list, angles: list, domain: list, title: str = 'Initial Capsule Distribution', show: bool = False, save: bool = False, font_style: str | None = None, show_title: bool = True, font_weight: str = 'bold', tick_interval: float | List[float] | Tuple[float, float] | None = None, show_particle_labels: bool = True)[源代码]
绘制初始胶囊/跑道形(Capsule)粒子分布的示意图 胶囊由一个矩形和两个半圆组成。
- 参数:
coordinates (list) -- 粒子几何中心坐标 [[x1, y1], ...]
radii (list) -- 胶囊两侧半圆的半径 [r1, r2, ...] (也即胶囊厚度的一半)
lengths (list) -- 胶囊中间直线段的长度 [L1, L2, ...] (总长度 = L + 2r)
angles (list) -- 旋转角度列表 [deg1, deg2, ...], 单位为度,逆时针为正。
domain (list) -- [width, height]
... (其他参数同 plot_initial_particle_schematic)
- boviz.schematic.plot_initial_particle_schematic(coordinates: list, radii: list, domain: list, title: str = 'Initial Particle Distribution', show: bool = False, save: bool = False, font_style: str | None = None, show_title: bool = True, font_weight: str = 'bold', tick_interval: float | List[float] | Tuple[float, float] | None = None, show_particle_labels: bool = True)[源代码]
绘制初始粒子分布的示意图。
- 参数:
coordinates (list) -- 粒子中心坐标列表,格式为 [[x1, y1], [x2, y2], ...]。
radii (list) -- 粒子半径列表,格式为 [r1, r2, ...]。
domain (list) -- 绘图区域的空间大小,格式为 [width, height]。
title (str) -- 图表标题。
show (bool) -- 是否显示图像,默认不显示。
save (bool) -- 是否保存图像,默认不保存。
font_style (str) -- 字体样式,默认为 Times。可选值为 'sans' 或 None。
show_title (bool, optional) -- 是否显示图片标题,默认显示。
font_weight (str) -- 字体粗细,默认为 "bold"。可选值为 'bold' 或 'normal'。
tick_interval (float | list | tuple, optional) -- 坐标轴刻度间隔。 如果为单个数值(如 20),则XY轴间隔相同; 如果为列表/元组(如 [50, 10]),则分别代表 [X轴间隔, Y轴间隔]。 如果为 None,则自动使用 smart ticks。
show_particle_labels (bool, optional) -- 是否在粒子中心显示编号,默认显示。
- boviz.schematic.plot_initial_superellipse_schematic(coordinates: list, semi_axes: list, exponents: list, domain: list, title: str = 'Initial Super-Ellipsoid Distribution', show: bool = False, save: bool = False, font_style: str | None = None, show_title: bool = True, font_weight: str = 'bold', tick_interval: float | List[float] | Tuple[float, float] | None = None, show_particle_labels: bool = True, resolution: int = 200)[源代码]
绘制初始超椭圆(Super-ellipse)粒子分布的示意图。
方程:
|x/a|^n + |y/b|^n = 1- 参数:
coordinates (list) -- 粒子中心坐标 [[x1, y1], ...]
semi_axes (list) -- 半轴长列表 [[a1, b1], [a2, b2], ...]
exponents (list) -- 形状指数 n 列表 [n1, n2, ...]。n=2为椭圆,n>2趋向矩形。
domain (list) -- [width, height]
resolution (int) -- 绘制曲线的离散点数量
... (其他参数同 plot_initial_particle_schematic)
boviz.style module
- boviz.style.apply_axis_limits_and_ticks(ax: Axes, curves: list[tuple[ndarray, ndarray]], xlim: tuple[float, float] | None = None, ylim: tuple[float, float] | None = None, tick_interval_x: float | None = None, tick_interval_y: float | None = None)[源代码]
设置 x/y 轴的显示范围与主刻度间隔。
- 参数:
ax -- matplotlib 的坐标轴对象。
curves -- 所有曲线的 (x, y) 数据对。
xlim -- x 轴显示范围 (min, max)。
ylim -- y 轴显示范围 (min, max)。
tick_interval_x -- x 轴主刻度间隔。
tick_interval_y -- y 轴主刻度间隔。
- 返回:
None
- boviz.style.apply_axis_scientific_format(ax, axis: str, scale: float, fontsize: int = 7.5)[源代码]
将某个轴设置为科学计数格式。
- 参数:
ax -- matplotlib 坐标轴对象。
axis (str) -- 'x' 或 'y'。
scale (float) -- 缩放因子。
fontsize (int) -- 标注字号。
- 返回:
None
- boviz.style.plot_residual_curves(ax_res, curves: list[tuple[ndarray, ndarray]], label: list[str], xy_label: tuple[str, str] | None = None, x_title_fallback: str | None = None)[源代码]
绘制残差图:将第 1 条曲线作为参考,绘制其与其他曲线之间的差值。
- 参数:
ax_res -- matplotlib 的残差子图坐标轴。
curves -- 所有曲线的 (x, y) 数据对。
label -- 每条曲线的标签。
xy_label -- 坐标轴标签元组,用于设置 x 轴名。
x_title_fallback -- 如果 xy_label 不存在时使用的 x 轴名。
- 返回:
None
- boviz.style.save_or_display_legend(ax, save_dir: str, figure_name_suffix: str, split_legend: bool = False, legend_location: str = 'best', legend_ncol: int = 1, dpi: int = 300, xy_label: list[str] | None = None, show_legend: bool = True)[源代码]
处理图例显示或保存为单独图像的逻辑。
- 参数:
ax -- 主图坐标轴。
save_dir -- 图像保存目录。
figure_name_suffix -- 用于生成图例文件名的后缀。
split_legend -- 是否将图例单独绘制保存。
legend_location -- 图例位置(默认 'best')。
legend_ncol -- 图例列数。
dpi -- 图像保存分辨率。
xy_label -- 用于命名的 x 和 y 轴标签列表。
show_legend -- 是否显示图例。
- 返回:
None
- boviz.style.set_ax_style(ax, linewidth=0.75)[源代码]
Set the style for a given Axes object.
- 参数:
ax (matplotlib.axes.Axes) -- The Axes object to style.
- 返回:
None
- boviz.style.set_cm_style(bold: bool = True)[源代码]
Set the default style for plots using Matplotlib's built-in Computer Modern ('cm') fonts. Does NOT require a local LaTeX installation.
- 参数:
bold (bool) -- Whether to use bold font weight.
- boviz.style.set_default_style(bold: bool = True)[源代码]
Set the default style for plots using serif fonts (Times New Roman).
- 参数:
bold (bool) -- Whether to use bold font weight.
boviz.utils module
- boviz.utils.build_tanh_phase_field(centers_coordinate: list, radii: list, domain_size: list, tanh_width: float = 3.0, tanh_offset: float = 0.05) ndarray[源代码]
构建基于双曲正切函数的相场。
- 参数:
centers_coordinate (list) -- 粒子中心坐标列表,每个元素为 [x, y]。
radii (list) -- 每个粒子的半径列表。
domain_size (list) -- 网格的域大小 [domain_x, domain_y]。
tanh_width (float) -- 双曲正切函数的宽度,默认值为 3.0。
tanh_offset (float) -- 双曲正切函数的偏移量,默认值为 0.05。
- 返回:
生成的相场数组,大小为 [domain_x * 10, domain_y * 10]。
- 返回类型:
np.ndarray
- boviz.utils.generate_particle_layout(num_x: int, num_y: int, radius: float, border: float | None = None) ndarray[源代码]
生成粒子布局的网格坐标。
- 参数:
num_x (int) -- X 方向的粒子数量。
num_y (int) -- Y 方向的粒子数量。
radius (float) -- 粒子的半径。
border (float, optional) -- 边界的宽度,默认为2倍颗粒半径。
- 返回:
- 包含三个元素的元组:
centers_coordinate (list): 粒子中心坐标的列表,每个元素为 [x, y]。
radii (list): 每个粒子的半径列表。
domain_size (list): 网格的域大小 [domain_x, domain_y]。
- 返回类型:
tuple
- boviz.utils.generate_plot_filename(title: str, file_format='png', suffix=None) str[源代码]
生成统一命名格式的图片文件名,格式为:boviz_YYMMDDHHMM_title_suffix.png
- 参数:
title (str) -- 图像标题或描述性名称(可含空格,会被自动替换为下划线)。
suffix (str, optional) -- 附加信息(如 "(test)"),默认空字符串。
- 返回:
构造后的图片文件名(不含路径)。
- 返回类型:
str
- boviz.utils.load_data_csv(source: str, x_index: int, y_index: int, factor: tuple[tuple, tuple], time_step: int = 0) tuple[ndarray, ndarray, str, str][源代码]
从 CSV 文件中读取指定列的数据。
- 参数:
source (str) -- CSV 文件路径。
x_index (int) -- X 轴数据的列索引。
y_index (int) -- Y 轴数据的列索引。
factor (tuple[touple, touple]) -- 用于缩放和平移 Y 轴数据的因子,格式为 [scale, offset]。 - scale: 缩放因子,默认为 1.0。 - offset: 平移量,默认为 0.0。
time_step (int) -- 若不为 0,则只保留前 time_step 个时间步。
- 返回:
对应列的数据(NumPy 数组) x_colname, y_colname: 对应列名
- 返回类型:
x_data, y_data
- boviz.utils.load_exodus_data(source: str, variable_name: str, time_step: int = 0)[源代码]
读取 Exodus 文件中的指定变量的指定时间步数据。
- 参数:
source (str) -- Exodus 文件路径。
variable_name (str) -- 要读取的变量名称。
time_step (int) -- 时间步索引,默认为 0。
- 返回:
网格节点坐标数组,形状为 (N, 2),其中 N 是节点数量。 variable_values (np.ndarray): 指定变量在指定时间步的值,形状为 (N,) 或 (N, M),其中 M 是变量的维度(如标量或向量)。
- 返回类型:
coordinates (np.ndarray)
- 抛出:
ValueError -- 如果指定的变量在 Exodus 文件中不存在。
FileNotFoundError -- 如果指定的 Exodus 文件不存在。
Module contents
boviz: A Python plotting toolkit for scientific visualization.
- This package exports:
plot_curves: Plotting of scientific curves with customizable styles.
plot_initial_particle_schematic: Visualization of initial particle schematics.
set_default_style: Set global matplotlib style for consistent appearance.
generate_plot_filename: Utility for generating plot filenames.
GLOBAL_COLORS, DEFAULT_SAVE_DIR, DEFAULT_DPI, DEFAULT_FIGSIZE: Global configuration constants.
Programs that import and use 'boviz' can easily create publication-quality plots with consistent styles and convenient utilities.
Author: Bo Qian Email: bqian@shu.edu.cn