garrettj403 / garrettj403/SciencePlots

[Feature] siunitx mplstyle

Open
#101 3 comments 1 reaction 0 assignees View on GitHub
enhancement good first issue
Dominant language
Python
Stars
9.2k
Forks
821
PR merge metrics
No merged PRs in 30d

Description

Hello, hope you are doing great.

There is this package [siunitx](https://ctan.org/pkg/siunitx?lang=en) which allows for a better usage of units in latex. This allows for different usage of units inside latex and to be more consistent in their use inside and outside of equations.

I believe it would be easy to implement in `SciencePlots` as an additional mplstyle.

# Proposal

For this it could be easy to create a `latex-siunitx.mplstyle` inside the `misc` folder with the line

```python
text.latex.preamble :\usepackage{amsmath} \usepackage{amssymb} \usepackage{siunitx}
```

# Example
```python
import numpy as np
import matplotlib.pyplot as plt
import scienceplots

x = np.linspace(0.75, 1.25, 201)

def model(x, p):
return x ** (2 * p + 1) / (1 + x ** (2 * p))
```

With the current implementation units would be like

```python
pparam = dict(xlabel='Voltage (mV)', ylabel=r'Current ($\mu$A)')

with plt.style.context(['science']):
fig, ax = plt.subplots()
for p in [10, 15, 20, 30, 50, 100]:
ax.plot(x, model(x, p), label=p)
ax.legend(title='Order')
ax.autoscale(tight=True)
ax.set(**pparam)
fig.savefig('fig01.png', dpi=300)
plt.close()
```

![fig01](https://github.com/garrettj403/SciencePlots/assets/2727433/6501d0b4-9b1c-4320-9223-ea8003c04c11)

with the new implementation it would change to

```python
pparam = dict(xlabel=r'Voltage (\si{\milli\volt})', ylabel=r'Current (\si{\micro\ampere})')

with plt.style.context(['science','latex-siunitx']):
fig, ax = plt.subplots()
for p in [10, 15, 20, 30, 50, 100]:
ax.plot(x, model(x, p), label=p)
ax.legend(title='Order')
ax.autoscale(tight=True)
ax.set(**pparam)
fig.savefig('fig01-siunitx.png', dpi=300)
plt.close()
```

![fig01-siunitx](https://github.com/garrettj403/SciencePlots/assets/2727433/74329a6e-f238-4a67-8616-ace9673f9230)

# Caveats

The only problem I seem to find with this is that if another style changes the `text.latex.preamble` line it would clash. It would be a problem for `nature`, `russian-font`, `turkish-font`, `latex-sans`, `pgf` and `sans`. Also it wouldn't work with styles that don't use latex like `notebook`.

Maybe this could be worked around in some way.

Best, Fran

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the proposed misc/latex-siunitx.mplstyle and review the existing nature, russian-font, turkish-font, latex-sans, pgf, sans, and notebook styles for text.latex.preamble interactions. Check the provided style-context example and determine how the new style should behave when combined with those styles. Done means the siunitx style works for the shown unit labels without breaking compatible existing styles.

Written by the indexing model from the issue text.

Assessment

Tech stack
latex, python
Domain
data-visualization
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.