1

I know that this question has been asked before here, but the answer given does not work for me.. When I put {} around my subscript, I am getting a Key Error. It happens whether I use "G${LA}$" or "G{LA}".

As an edit, this is a simplified version of the code I'm using:

X=np.array(random.sample(range(1000),10))
Y=np.array(random.sample(range(1000),10))

plt.clf()
f, ax =plt.subplots(1,1)

b, a = np.polyfit(X,Y, 1)

# 3.5 Calculate Pearson's correlation
r, SE = stats.pearsonr(X, Y)
r2 = r*r

# 3.6 Regression line to be added to plot - anchored text automatically locates the text, without the need to specifying the x,y coordinates
anchored_text = AnchoredText("GLA = {:3.2f}*GLD + {:3.2f}\n\nPearson's R = {:3.2f}, SE = {:3.2f}\n $R^2$ = {:3.2f}".format(b,a,r,SE,r2), loc=2)

# 3.7 Plot
ax = sns.regplot(X, Y, color='g')
ax.add_artist(anchored_text)

plt.show()

Now, if I add the subscript, it throws in the error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-31-00e10c71309f> in <module>()
     19 
     20 # 3.6 Regression line to be added to plot - anchored text automatically locates the text, without the need to specifying the x,y coordinates
---> 21 anchored_text = AnchoredText(r"G$_{LA}$ = {:3.2f}*GLD + {:3.2f}\n\nPearson's R = {:3.2f}, SE = {:3.2f}\n $R^2$ = {:3.2f}".format(b,a,r,SE, r2), loc=2)
     22 
     23 # 3.7 Plot

KeyError: 'LA'

Would that be because I am using AnchoredText? Or has anything changed since the other thread was posted?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
branwen85
  • 1,606
  • 5
  • 20
  • 25
  • Possibly... still the same error remains though. – branwen85 Jul 10 '17 at 11:35
  • Can you provide the code you use? Better yet a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – DavidG Jul 10 '17 at 11:36
  • What if you write: `format(10, b,a,r,SE, r2)`. I wonder if the `{}` is being understood as a part of string format?! – Nipun Batra Jul 10 '17 at 11:38
  • @NipunBatra You might be right. If you remove everything else and just have `r"$G_{LA}$"` then the problem goes away. – DavidG Jul 10 '17 at 11:42
  • @DavidG: In that case maybe just write as: `AnchoredText(r"G$_{}$ = {:3.2f}*GLD + {:3.2f}\n\nPearson's R = {:3.2f}, SE = {:3.2f}\n $R^2$ = {:3.2f}".format("LA", b,a,r,SE, r2), loc=2)` – Nipun Batra Jul 10 '17 at 11:44
  • @NipunBatra Yeah I thought that too but then only the `L` is a subscript – DavidG Jul 10 '17 at 11:47
  • @DavidG - I have added the sample code – branwen85 Jul 10 '17 at 11:52
  • @NipunBatra I have tried both suggestions, but the best I can do (after moving LA to the parameters after format) is to get the L as subscript, but not A.. – branwen85 Jul 10 '17 at 11:53

1 Answers1

3

As @NipunBatra said in the comments it seems that the {LA} is being understood as a part of the string format. In order to solve this add and extra pair of curly braces around {LA}. Your anchored_text would then become:

anchored_text = AnchoredText(r"G$_{{LA}}$ = {:3.2f}*GLD + {:3.2f}\n\nPearson's R ="
                             r" {:3.2f}, SE = {:3.2f}\n $R^2$ = {:3.2f}".format(b,a,r,SE, r2), loc=2)
DavidG
  • 24,279
  • 14
  • 89
  • 82