diff --git a/alpsplot/__init__.py b/alpsplot/__init__.py index 86bcb68..34e8bb1 100644 --- a/alpsplot/__init__.py +++ b/alpsplot/__init__.py @@ -12,4 +12,4 @@ __all__ = ['colormap', 'Figure', 'utils'] fonts.main() -plt.style.use(os.path.join(os.path.dirname(__file__), 'alpsplot.mplstyle')) +plt.style.use(os.path.join(os.path.dirname(__file__), 'optima.mplstyle')) diff --git a/alpsplot/figure.py b/alpsplot/figure.py index 8f22599..14c1ca7 100644 --- a/alpsplot/figure.py +++ b/alpsplot/figure.py @@ -331,7 +331,7 @@ def set_axis_label(self, axis: str, text: str, **kwargs): def set_axis_lim(self, axis: str, labels: list[str] = None, lim: tuple[float, float] = (0.0, 1.0), - margin: tuple[float, tuple] = (0.0, 0.0), + margin: float | tuple[float, float] = 0.0, piece: int = 10, _format: str = None, **kwargs): r"""Set ticks and their labels for axis. @@ -366,9 +366,9 @@ def set_axis_lim(self, axis: str, labels: list[str] = None, Defaults to ``None``. lim (tuple[str, str]): The limit of axis ticks. Defaults to ``(0.0, 1.0)``. - margin (tuple[str, str]): The margin at + margin (float | tuple[str, str]): The margin at head and tail of axis ticks. - Defaults to ``(0.0, 0.0)``. + Defaults to ``0.0``. piece (int): The number of axis ticks - 1. The interval among ticks are :math:`\frac{\text{lim}[1] - \text{lim}[0]}{\text{piece}}`. @@ -403,6 +403,8 @@ def set_axis_lim(self, axis: str, labels: list[str] = None, :width: 60% """ # noqa: E501 + if isinstance(margin, float): + margin = (margin, margin) final_lim = lim[0] - margin[0], lim[1] + margin[1] if piece == 0: ticks = np.array([lim[0]]) diff --git a/alpsplot/optima.mplstyle b/alpsplot/optima.mplstyle new file mode 100644 index 0000000..0be1878 --- /dev/null +++ b/alpsplot/optima.mplstyle @@ -0,0 +1,18 @@ +figure.titlesize: 18 +axes.titlesize: 18 +axes.labelsize: 18 +xtick.labelsize: 16 +ytick.labelsize: 16 +legend.fontsize: 16 +legend.title_fontsize: 16 + +axes.titleweight: regular +axes.labelweight: regular + +font.weight: regular +font.family: Optima + +pdf.fonttype: 42 +ps.fonttype: 42 +svg.image_inline : True +svg.fonttype: none diff --git a/alpsplot/alpsplot.mplstyle b/alpsplot/optima_bold.mplstyle similarity index 100% rename from alpsplot/alpsplot.mplstyle rename to alpsplot/optima_bold.mplstyle diff --git a/docs/source/examples/math.rst b/docs/source/examples/math.rst new file mode 100644 index 0000000..bd2ac4b --- /dev/null +++ b/docs/source/examples/math.rst @@ -0,0 +1,47 @@ +math +================================== + +.. image:: /images/examples/math.svg + :width: 100% + +.. code-block:: python + + import math + import numpy as np + + import alpsplot + from alpsplot.colormap import ting_color + + x = np.arange(-2*math.pi, 2*math.pi, step=1e-2) + y1 = np.sin(x) + y2 = 2*np.sin(3*x-math.pi/6) + + figure = alpsplot.Figure(figsize=(10, 5)) + figure.ax.grid(axis='both', linewidth=1, alpha=0.5) + figure.ax.axhline(0, color='black', linewidth=1) + figure.ax.axvline(0, color='black', linewidth=1) + + figure.set_title('24年高考新一卷第7题', font='Microsoft YaHei', size=25) + figure.set_axis_label(axis='x', text='x') + figure.set_axis_label(axis='y', text='y') + figure.set_axis_lim( + 'x', + lim=[-2*math.pi, 2*math.pi], + margin=0.3, + piece=12, + labels=[ + r'$-2\pi$', r'$-\frac{5}{3}\pi$', r'$\frac{4}{3}\pi$', + r'$-\pi$', r'$-\frac{2}{3}\pi$', r'$-\frac{1}{3}\pi$', + r'$0$', + r'$\frac{1}{3}\pi$', r'$\frac{2}{3}\pi$', r'$\pi$', + r'$\frac{4}{3}\pi$', r'$\frac{5}{3}\pi$', r'$2\pi$', + ], + ) + figure.set_axis_lim('y', lim=[-2, 2], margin=(0.1, 0.75), piece=4) + figure.lineplot(x, y1, label=r'$\sin(x)$', color=ting_color['red']) + figure.lineplot( + x, y2, label=r'$2\sin(3x-\frac{\pi}{6})$', color=ting_color['blue']) + figure.set_legend(ncols=2, loc='upper center') + figure.save(ext='.svg') + + diff --git a/docs/source/images/examples/math.svg b/docs/source/images/examples/math.svg new file mode 100644 index 0000000..a6be442 --- /dev/null +++ b/docs/source/images/examples/math.svg @@ -0,0 +1,958 @@ + + + + + + + + 2024-06-11T18:13:33.398624 + image/svg+xml + + + Matplotlib v3.8.4, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + −2 + π + + + + + + + + + + + + + + + + + + + 53 + π + + + + + + + + + + + + + + + + + + + 43 + π + + + + + + + + + + + + + + + + + + + + π + + + + + + + + + + + + + + + + + + + 23 + π + + + + + + + + + + + + + + + + + + + + 13 + π + + + + + + + + + + + + + + + + + + + 0 + + + + + + + + + + + + + + + + + + 13 + π + + + + + + + + + + + + + + + + + + + 23 + π + + + + + + + + + + + + + + + + + + + π + + + + + + + + + + + + + + + + + + 43 + π + + + + + + + + + + + + + + + + + + + 53 + π + + + + + + + + + + + + + + + + + + + 2 + π + + + + + + x + + + + + + + + + + + + + + + + + −2 + + + + + + + + + + + + + −1 + + + + + + + + + + + + + 0 + + + + + + + + + + + + + 1 + + + + + + + + + + + + + 2 + + + + y + + + + + + + + + + + + + + + + + + + 24年高考新一卷第7题 + + + + + + + + + + + + + sin() + x + + + + + + + + + + + 2sin(3−) + x + π + 6 + + + + + + + + + + + + + diff --git a/docs/source/index.rst b/docs/source/index.rst index cbc79af..05796ce 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -1,9 +1,20 @@ -AlpsPlot documentation +Examples ====================== .. toctree:: - :caption: AlpsPlot Documentation + :caption: Examples + :titlesonly: + :maxdepth: 1 + + examples/math + + +Documentation +====================== + +.. toctree:: + :caption: Documentation :titlesonly: :maxdepth: 2