garrettj403/SciencePlots

Can't line break with \n in ax.set_ylabel.

simonmarti1992 opened this issue · 4 comments

When I use the package, I can no longer write texts with accents and line breaks in my y-labels

System: Windows-10-10.0.22631-SP0
Python: 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)]
SciencePlots: 2.1.1
Matplotlib: 3.7.2
Latex distro: (populate manually)

I tried several common tricks for strings in y-labels like ax.set_ylabel(r"MyTest"), but it didn't work

I haven't done any testing, but cmon, what you are feeding into Matplotlib is the LaTeX you want to render. How do you insert a line break in LaTeX?

https://www.overleaf.com/learn/latex/Line_breaks_and_blank_spaces#Line_breaks

Feel free to close when resolved.

Thank you for your feedback, but it doesn't answer my question. The error was due to improper use of the 'r' at the beginning of the string. Here's an example that works

import scienceplots
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use(['science','no-latex','grid'])
iris = sns.load_dataset('iris')
iris.head()

fig, (ax) = plt.subplots(1,1, figsize = (15,5))
ax.plot(iris.index, iris.sepal_length)
ax.set_ylabel('my \n test')
image

And here is the case that is not wordking properly

import scienceplots
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use(['science','no-latex','grid'])
iris = sns.load_dataset('iris')
iris.head()

fig, (ax) = plt.subplots(1,1, figsize = (15,5))
ax.plot(iris.index, iris.sepal_length)
ax.set_ylabel(r'my \n test')
image

Thanks for reporting the solution.