I'm using matplotlib to produce some graphics, and I'm using latex for the legends.
More specifically, I'm trying to use something like this:
loglog(x,x, '-r',label='$ \alpha $')
legend()
show()
However, this code does not present a legend on the figure, and gets error after I close the image.
I'm using the enthought package (for mac), but the error comes from the pylab/scipy.
The error the appears is exactly:
$ lpha $ (at char 0), (line:1, col:1)
However, if use the \mu or \gamma, it works well!! I only found about this problem on \beta and \alpha.
Does anyone knows what this can be? I believe python is interpreting "\a" as some character... but I don't know how should I debug / avoid it.
The issue is that \a and \b have special meaning inside a Python string literal.
I recommend that you use raw strings whenever there is a backslash embedded inside a string:
r'$ \alpha $'
In addition to using raw strings (as mentioned in the post above) you can also escape the backslash. So typing \\alpha and \\beta will also work.
Hi if your \alpha and \beta don't work on Latex
use $\alpha$
likewise $\beta$ so on.
hope it works
Related
autopep8 converts this code
sum_of_two_numbers = first \
+ second
to this
sum_of_two_numbers = first \
+ second
Is it a bug? If so, is it a bug in pycodestyle or autopep8? Are there any error codes I can ignore to prevent this behavior? If I ignore E127 and E128 it also stops indenting all other cases.
I know that if I use brackets instead of backslash it will work correctly, however, there is an existing repository that uses backslashes in some places which I do not want to change.
UPD. Adding another example from pep8 (https://www.python.org/dev/peps/pep-0008/#maximum-line-length)
Backslashes may still be appropriate at times. For example, long,
multiple with-statements cannot use implicit continuation, so
backslashes are acceptable:
with open('/path/to/some/file/you/want/to/read') as file_1, \
open('/path/to/some/file/being/written', 'w') as file_2:
file_2.write(file_1.read()) ```
autopep8 does not align this example correctly, too.
To my knowledge, you cannot disable just this feature without disabling other auto-indenting. If you are running autopep8 that implies that you are trying to follow pep8 guidelines, in the case of the example you should really be using implied continuation (no backslash, '+' on the previous line, 4 space indent.) If you are using autopep8 on a project you have to be willing to let autopep8 format the project for you. I'm not sure what the attachment to backslashes is here, but it's probably not the nicest way to format what you're doing.
I'm using VSCode Version: 1.46.1 on Mac OS Catalina. I'm using the built-in Python interactive terminal Python 3.7.4 Whenever I print strings, it shows up with unicode, making it difficult to read, like so:
\\u201cI like what we have.\\u201d It is quiet and there is somebody else in the room. I tell my dog that I need to go and he says, \\u201cjust alright.\\u201d ~~I am hungry.\\n\\n
I have tried every flavor of un-escaping escaped characters. See here:
Unescaping escaped characters in a string using Python 3.2
And
Using unicode character u201c
But to no avail. I think the problem lies in the encoding options built into VSCode itself, but I'm not sure how to modify that.
Maybe this page could provide some information for you.
"\u201c" and "\u201d" means “ and ”, but they will not work, they should be "\u201c" and "\u201d".
using re.escape() on this directory:
C:\Users\admin\code
Should theoratically return this, right?
C:\\Users\\admin\\code
However, what I actually get is this:
C\:\\Users\\admin\\code
Notice the backslash immediately after C. This makes the string unusable, and trying to use directory.replace('\', '') just bugs out Python because it can't deal with a single backslash string, and treats everything after it as string.
Any ideas?
Update
This was a dumb question :p
No it should not. It's help says "Escape all the characters in pattern except ASCII letters, numbers and '_'"
What you are reporting you are getting is after calling the print function on the resulting string. In console, if you type directory and press enter, it would give something like: C\\:\\\\Users\\\\admin\\\\code. When using directory.replace('\\','') it would replace all backslashes. For example: directory.replace('\\','x') gives Cx:xxUsersxxadminxxcode. What might work in this case is replacing both the backslash and colon with ':' i.e. directory.replace('\\:',':'). This will work.
However, I will suggest doing something else. A neat way to work with Windows directories in Python is to use forward slash. Python and the OS will work out a way to understand your paths with forward slashes. Further, if you aren't using absolute paths, as far as the paths are concerned, your code will be portable to Unix-style OSes.
It also seems to me that you are calling re.escape unnecessarily. If the printing the directory is giving you C:\Users\admin\code then it's a perfectly fine directory to use already. And you don't need to escape it. It's already done. If it wasn't escaped print('C:\Users\admin\code') would give something like C:\Usersdmin\code since \a has special meaning (beep).
How do I create a SymPy symbol that has a multi-character string for a subscript which renders properly with pretty printing?
The following render correctly:
symbols('tau_12')
symbols('tau_x')
but I cannot get the following to render the subscript correctly:
symbols('tau_xy')
How do I get a multi-character subscript?
Is this what you want?
xi1 = Symbol('x_i^{(1)}')
xi1
The problem is that Unicode only has a limited set of characters as subscripts (see https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts#Other_superscript_and_subscript_characters). In particular, there is no Unicode character for subscript y. Your best bet if you want prettier printing is to use the IPython notebook or qtconsole, where you can get rendered math using MathJax or LaTeX.
I'm trying to add some axis-labels to a graph which contains the Greek letter 'rho'. To do this I want to use the LaTeX capability of Matplotlib but it seems to have a problem with the \rho symbol.
Here is a minimal example:
import matplotlib.pyplot as plt
from matplotlib import rc,rcParams
rc('text',usetex=True)
rcParams.update({'font.size': 16})
plt.plot([0,1,2,3,4],[0,1,4,9,16])
plt.xlabel('\rho A_i') # works if \rho is replaced with, for example, \sigma
plt.ylabel('Something else')
plt.show()
Upon running the first time I get a bunch of LaTeX errors and a blank figure window, running again shows the graph but the xlabel reads 'ho Ai ' where the i is subscript as expected.
The weird thing is if I replace \rho with something else, say, \sigma it shows up correctly. Can anybody tell me why it is not happy with my code example and how to fix it?
Thanks.
P.s. I tried putting the expression in $..$ but that changed nothing.
I think you are supposed to use raw strings, and use the $ signs as well. Try:
plt.xlabel(r'$\rho A_i$')
Be careful when you're using \n , \r and so on in a string. Those are commands for e.g. entering a new line etc.
https://docs.python.org/2/library/re.html
To make sure you don't use these regular expression operators put \\rho instead of \rho.