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'