3

I would like to include the equation for a "power-law" curve I am creating using pyplot. I have tried several variations of the following code:

ax.text(0.1,0.9,r'{0:}x$^{1:}$'.format(A,b))

of course the text renderer uses the curly brackets as well as the format statement. I have tried doubling the curly brackets to have

ax.text(0.1,0.9,r'{0:}x$^{{1:}}$'.format(A,b))

and even tripling them

ax.text(0.1,0.9,r'{0:}x$^{{{1:}}}$'.format(A,b))

I have also tried splitting the text into two lines

exponent = '{0:}$'.format(b)
ax.text(0.1,0.9,r'{0:}x$^'.format(A)+exponent)

none of these really make sense to me or to pyplot, but I can't seem to ask the right search question to get this to work. I have found answers that suggested splitting the line and using double curly brackets but nothing that will make this work. Is this possible?

EDIT:

Through further experimentation I have answered the question above which was a simplified version of the equation I wanted to put in the plot, so let me change this question slightly. To do what I wanted above I have found that:

ax.text(0.1,0.9,r'{0:}x$^{{ {1:} }}$'.format(A,b))

works. I don't know why I hadn't tried it earlier, but I have now. The problem is that I actually want a subscript on x as well. Given that the above works I would have thought that:

ax.text(0.1,0.9,r'{0:}x$^{{ {1:} }}_{{\rm map}}$ 

would work, but I get the following error:

Subscript/superscript sequence is too long. Use braces { } to remove ambiguity. (at char 0), (line:1, col:1)

I cannot see where to add braces that will remove any ambiguity. Can anyone tell me what I need to do?

1
  • Your last line of code is incomplete. Commented Mar 16, 2016 at 11:00

1 Answer 1

3

This works:

ax.text(0.1, 0.9, r'${0:}x^{{{1:}}}_{{\rm map}}$'.format(A, b))

The issue was that your x was outside of the mathmode ($...$).

With regards to the double curly braces, there is an easy explanation: When using the format function all single curly braces are matched with an argument to the format function (they can also be nested). Two subsequent curly braces are the defined way of getting one curly bracket after applying format. See the documentation for more information on this.

Sign up to request clarification or add additional context in comments.

1 Comment

This is interesting, I often keep the variable outside the $$ in order to avoid the need for the \rm, so it didn't occur to me to try that. Apparently it is necessary in this case!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.