A utility library for matplotlib plotting configuration
Project description
PltSci
一个用于简化 matplotlib 绘图参数设置的 Python 工具库。
安装
pip install pltsci
快速开始
示例:创建一个全宽的科学绘图
from pltsci import whole_plot_set, set_ticks, cm
import matplotlib.pyplot as plt
import numpy as np
# 设置全局绘图参数
whole_plot_set() # 全宽图片参数设置
fig, ax = plt.subplots(figsize=(15 * cm, 10 * cm), dpi=300) # 创建图形 (使用厘米单位)
# 创建示例数据
x = np.linspace(0, 10, 100)
y = np.sin(x)
ax.plot(x, y, label="$y=\\sin(x)$")
# 设置坐标轴范围和刻度
set_ticks(ax, xrange=(0, 10, 2), yrange=(-1.5, 1.5, 0.5))
# 添加标签和图例
ax.set_xlabel("x", fontsize=12)
ax.set_ylabel("y", fontsize=12)
ax.legend()
plt.tight_layout()
fig.savefig("examples/whole_plot.svg", bbox_inches="tight")
结果:
示例:创建一个半宽的科学绘图
from pltsci import whole_plot_set, half_plot_set, set_ticks, cm
import matplotlib.pyplot as plt
import numpy as np
# 设置全局绘图参数
whole_plot_set() # 全宽图片参数设置,即使是用半宽图,也建议先调用此函数设置全局参数
fig, ax = plt.subplots(figsize=(7 * cm, 5 * cm), dpi=300) # 创建图形 (使用厘米单位)
half_plot_set(ax) # 半宽图片参数设置
# 创建示例数据
x = np.linspace(0, 10, 100)
y = np.exp(x)
ax.plot(x, y, label="$y=e^x$")
# 设置坐标轴范围和刻度
set_ticks(ax, xrange=(0, 10, 2), yrange=(0, 22000, 5000))
# 添加标签和图例
ax.set_xlabel("x", fontsize=10)
ax.set_ylabel("y", fontsize=10)
ax.legend(fontsize=8)
plt.tight_layout()
fig.savefig("examples/half_plot.svg", bbox_inches="tight")
# fig.savefig("examples/half_plot.jpg", bbox_inches="tight", dpi=1200) # 位图记得设置高分辨率
结果:
API 参考
whole_plot_set(font=None, math_font="stix")
设置全局绘图参数,包括字体、刻度方向、图例样式等。
font: 字体列表,默认为["Times New Roman", "SimSun"]math_font: 数学公式字体,默认为"stix"
set_ticks(ax, xrange=None, yrange=None)
设置坐标轴范围和刻度。
ax: matplotlib 轴对象xrange: x轴范围,格式为(xmin, xmax, xstep)yrange: y轴范围,格式为(ymin, ymax, ystep)majorMaxNLocator: 主刻度最大数量,默认为NoneminorLocator: 每两个主刻度之间的次刻度数量,默认为 2have_x_major: 是否显示 x 轴主刻度,默认为 Truehave_y_major: 是否显示 y 轴主刻度,默认为 Truehave_x_label: 是否显示 x 轴刻度值,默认为 Truehave_y_label: 是否显示 y 轴刻度值,默认为 True
half_plot_set(ax)
设置坐标轴线宽和刻度样式,适用于密集布局的图表。
ax: matplotlib 轴对象
cm
厘米到英寸转换工具。
# 两种使用方式
fig, ax = plt.subplots(figsize=(15 * cm, 10 * cm))
# 或者
fig, ax = plt.subplots(figsize=(cm(15), cm(10)))
许可证
MIT License - 详见 LICENSE 文件。
贡献
欢迎提交 Issue 和 Pull Request!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pltsci-0.2.0.tar.gz
(5.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pltsci-0.2.0.tar.gz.
File metadata
- Download URL: pltsci-0.2.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63e774fd0f7377c84b43571ac64b93ce72368c120bc0e81aab6c523a5ec540f5
|
|
| MD5 |
f3292ff2a9520d3cb0a5fa9e870a2e53
|
|
| BLAKE2b-256 |
31af17d0b180a374e2304c1033fa9343302bd931089b9e85456ab7331ebfa343
|
File details
Details for the file pltsci-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pltsci-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c1ad9910d9bd8bc279a9988f6e87aa5e2fb1937a7f524cf39bde46ea98d1f27
|
|
| MD5 |
dc1b725c148b845aea49724743941d28
|
|
| BLAKE2b-256 |
d4ee9b35939cf9316f2074e2c93ddf9a4c9dfe4b91f7403770cfab9bd8d503ce
|