garrettj403 / SciencePlots

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

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

Closed simonmarti1992 closed 7 months ago

simonmarti1992 commented 7 months ago

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

echedey-ls commented 7 months ago

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

echedey-ls commented 7 months ago

Feel free to close when resolved.

simonmarti1992 commented 7 months ago

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
echedey-ls commented 7 months ago

Thanks for reporting the solution.