Skip to main content

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")

结果:

whole_plot_example

示例:创建一个半宽的科学绘图

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) # 位图记得设置高分辨率

结果:

half_plot_example

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: 主刻度最大数量,默认为None
  • minorLocator: 每两个主刻度之间的次刻度数量,默认为 2
  • have_x_major: 是否显示 x 轴主刻度,默认为 True
  • have_y_major: 是否显示 y 轴主刻度,默认为 True
  • have_x_label: 是否显示 x 轴刻度值,默认为 True
  • have_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.2.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pltsci-0.2.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file pltsci-0.2.2.tar.gz.

File metadata

  • Download URL: pltsci-0.2.2.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for pltsci-0.2.2.tar.gz
Algorithm Hash digest
SHA256 51acf97e47a79df6f6a088e66e817bfd6d6435d3039c07bd051884b059205d04
MD5 3c25dfe64fae4bbfe0fea4c7904f8572
BLAKE2b-256 fefa4e2fc848006ef48698b3490d99630dda097c00db931c64b36968a502870a

See more details on using hashes here.

File details

Details for the file pltsci-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: pltsci-0.2.2-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

Hashes for pltsci-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 431ac7c9bcb714a6db461afdc14ff185fc109607b6443866ed2451754213b06a
MD5 fb652bf7c20df1bf26e8e46d68ea1cf3
BLAKE2b-256 7719b4693dceac6d01064573e6b88e1c7e5500341f6b0fdab872fd422c59ee16

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page