garrettj403 / SciencePlots

Matplotlib styles for scientific plotting
MIT License
6.96k stars 700 forks source link

[Bug]: Rendering bug with no-latex #93

Closed JBorrow closed 1 year ago

JBorrow commented 1 year ago

Environment

System: macOS-13.3.1-arm64-arm-64bit
Python: 3.9.7 (default, Aug 31 2021, 21:51:10)
[Clang 12.0.5 (clang-1205.0.22.9)]
SciencePlots: 2.0.1
Matplotlib: 3.6.2

Describe the issue here

Using the no-latex stylesheet provides very different rendering to those that require a TeX distribution.

Throughout, I will use the following test script:

import matplotlib.pyplot as plt
import scienceplots

plt.style.use([$STYLESHEETS])

plt.plot(range(10), range(10), label="Test")
plt.legend()
plt.xlabel("$x$-axis [Mpc]")
plt.ylabel("$f(x) = \\frac{x}{x^2} x^2$ [Mpc]")

plt.savefig("test.png", dpi=300)

For $STYLESHEETS = "science", we get the following:

test

Where all of the labels are rendered using the default TeX font.

But when using $STYLESHEETS = "science", "no-latex", we get the following: test

Where the fonts are now replaced by the matplotlib defaults (in this case Dejavu Serif).

In my opinion, the package should strive to keep rendering as close as possible between scenarios where TeX is used and where it is not.

By modifying the no-latex.mplstyle stylesheet to be the following:

# Deactivate LaTeX

text.usetex : False

# Set fonts to be LaTeX defaults
font.serif : cmr10, Computer Modern Serif, DejaVu Serif
font.family : serif
axes.formatter.use_mathtext : True
mathtext.fontset : cm

We can ensure that the rendering looks as close as possible: test

This should never fail, as cm and cmr10 are TTFs that are bundled with matplotlib itself (they reside in mpl-data).

Now in reality setting the correct fonts should not be the no-latex stylesheet's responsibility.

In particular to remedy this bug you would want to change in science.mplstyle:

font.serif : cmr10, Computer Modern Serif, DejaVu Serif
axes.formatter.use_mathtext : True
mathtext.fontset : dejavuserif

Or if you wanted to use the Times family:

font.family: STIXGeneral
mathtext.fontset: stix
echedey-ls commented 1 year ago

I would love to see a PR on this!

Thank you very much for that information.