Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
ain-soph committed Jun 11, 2024
1 parent 1af5611 commit 76e8687
Show file tree
Hide file tree
Showing 7 changed files with 1,042 additions and 6 deletions.
2 changes: 1 addition & 1 deletion alpsplot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
8 changes: 5 additions & 3 deletions alpsplot/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}}`.
Expand Down Expand Up @@ -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]])
Expand Down
18 changes: 18 additions & 0 deletions alpsplot/optima.mplstyle
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
47 changes: 47 additions & 0 deletions docs/source/examples/math.rst
Original file line number Diff line number Diff line change
@@ -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')
Loading

0 comments on commit 76e8687

Please sign in to comment.