0

I have some problem about .format() when I use it in matplotlib plotting: Suppose, I have a matrix A = np.column_stack([[0, 1], [-1, 0]]) and I have to display this matrix in my plot. Also this matrix may be changed as per my need. So I use the following code but it gives error. Can you please tell me how to do this task. Also I faced a similar problem when I try to display a vector like, $2\hat{i}+5 \hat{j}$:

I try the following for the matrix:

A = np.column_stack([[0, 1], [-1, 0]])
matrix = r'$\left( \begin{array}{ll} {a11} & {a12} \\ {a21} & {a22} \end{array} \right)$'.format(a11=A[0][0], a12=A[0][1], a21=A[1][0], a22=A[1][1])
plt.title(matrix)

Output:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-89-12cfe5180092> in <module>()
     12 
     13 A = np.column_stack([[0, 1], [-1, 0]]) #Matrix
---> 14 title = r'$\left( \begin{array}{ll} {a11} & {a12} \\ {a21} & {a22} \end{array} \right)$'.format(a11=A[0][0], a12=A[0][1], a21=A[1][0], a22=A[1][1])
     15 
     16 #Plotting:

KeyError: 'array'

I try the following for the vector:

x = [2,3]
plt.text(2,3,'${a}\hat{i}+{b}\hat{j}$'.format(a= x[0], b=x[1]))

Output:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-92-31281b09369d> in <module>()
     32 
     33 
---> 34 plt.text(2,3,'${a}\hat{i}+{b}\hat{j}$'.format(a= x[0], b=x[1]))
     35 
     36 #plt.title(title, y = 1.03)

KeyError: 'i'
sigma
  • 227
  • 3
  • 13
  • 2
    What's the error? – cheersmate Sep 25 '18 at 06:49
  • It gives : ```KeyError: 'array'``` – sigma Sep 25 '18 at 06:51
  • 1
    As the error says, you are not passing a keyword argument of `array` to the `format` method and yet you have `{array}` in your formatting string. – blhsing Sep 25 '18 at 06:52
  • @blhsing...then how to fix it.. – sigma Sep 25 '18 at 06:53
  • You define "Things ... {array} ... " and then call '.format(...)' on it. Then you must tell '.format(array="Value)' to fill in the '{}'. – MadMike Sep 25 '18 at 07:03
  • @MadMike...I don't get you... – sigma Sep 25 '18 at 07:12
  • "My name is {name}, I live in {place}".format(name="MadMike", place="Europe") #Every {} within the String is placeholder. The call '.format()' expects you to him a value. You are using placeholders where you don't give a value and python tells you so – MadMike Sep 25 '18 at 07:34
  • But see that I have given the value also......```.format(a11 = A[0][0]..)```..still it gives error – sigma Sep 25 '18 at 07:39
  • But you don't have `array=value` in your `.format()`... – tda Sep 25 '18 at 07:58
  • I added it like.format(array=value, a11=..)....but it gives: NameError: name 'value' is not defined – sigma Sep 25 '18 at 08:03
  • Yes because you do not have a variable set to `value`. It was an example. You need to replace 'value' with whatever you need `array` to be set to. See my answer for further clarity. – tda Sep 25 '18 at 08:04
  • There are two problems. (1) matplotlib mathtext does not support environments, i.e. in order to use `\begin{array}` you need to use true tex (2) Curly brackets need to be escaped if they are not be used as format placeholders. I closed as duplicate of respective questions. – ImportanceOfBeingErnest Sep 25 '18 at 12:13

1 Answers1

0

I've used your second example to provide an answer for simplicity to explain the issue.

x = [2,3]
plt.text(2,3,'${a}\hat{i}+{b}\hat{j}$'.format(a= x[0], b=x[1]))

This returns a KeyError: 'i' because the array variable specified in the string as {i} has not been set in your .format() statement. It was also fail if you just add i to your .format() statement as j has not been specified either.

See the following for a working example:

x = [2,3]
plt.text(2,3,'${a}\hat{i}+{b}\hat{j}$'.format(a= x[0], b=x[1], i="SOMETHING", j="SOMETHING"))

SUMMARY: Everything inside a {} needs to be set in your .format() statement.

tda
  • 2,045
  • 1
  • 17
  • 42