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.1.tar.gz (5.2 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.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

Hashes for pltsci-0.2.1.tar.gz
Algorithm Hash digest
SHA256 dc99625c84d3eae83698c9d419583c26b160339c9a468c318a89d2d3bc1128b7
MD5 78e519005e9a9482687d630fa6ff6d23
BLAKE2b-256 22aa7ebb72f30ca03c39c443aef09664f3accaabe68a6d03a49441b63b7056db

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pltsci-0.2.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7cc28acbe1c2ea8f0df55d3cace5fc80c849f5f4c007c5dd2e0422f72fddba35
MD5 38d3e36059b6c606793a5f4a84fdcdf1
BLAKE2b-256 84a97ff0244a1a3f38f348add348c58ab41c7a89d2f5c9cd4e12fb7c7a330cc8

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