Percentage format in elements of python graph
<p>Im executing the below code and I would like the numbers in the second graph to be percentage format with a two digit precision (0.3333 --> 33.33%). I have tried a ton of different version where I use '{percent, .2%}'.format() in lambda functions on the arrays, etc, but I dont get it all the way. All input is appriciated!</p>

<pre><code>import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets

%matplotlib inline

iris = datasets.load_iris()
x = iris['data']
y = iris['target']

x = iris_x[:, :2]

clf_tree = DecisionTreeClassifier(random_state = 1)
fit_clf = clf_tree.fit(x, y)

y_pred_proba = fit_clf.predict_proba(x)
y_pred = fit_clf.predict(x)

conf_mat = confusion_matrix(y_true = y, y_pred = y_pred)

fig, ax = plt.subplots(figsize = (15, 9))
ax.matshow(conf_mat, cmap = plt.cm.Blues, alpha = 0.3)

for i in range(conf_mat.shape[0]):
    for j in range(conf_mat.shape[1]):
        ax.text(x = j, y = i,
               s = conf_mat[i, j],
               va = 'center',
                ha = 'center')

plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()


conf_mat_prc = conf_mat/len(y)

fig, ax = plt.subplots(figsize = (15, 9))
ax.matshow(conf_mat_prc, cmap = plt.cm.Blues, alpha = 0.3)


for i in range(conf_mat_prc.shape[0]):
    for j in range(conf_mat_prc.shape[1]):
        ax.text(x = j, y = i,
               s = conf_mat_prc[i, j],
               va = 'center',
                ha = 'center')

plt.xlabel('Predicted % dist')
plt.ylabel('Actual % dist')
plt.show()
</code></pre>

<p>Many thanks in advance,</p>

<p>--swepab</p>

<p>There are (at least) two problems in your code: </p>

<ul>
<li><p>What is <code>iris_x</code> in <code>line 14</code>? I think you meant <code>x[:, :2]</code> instead of <code>iris_x[:, :2]</code></p></li>
<li><p><code>conf_mat_prc</code> should be defined as <code>conf_mat_prc = conf_mat/float(len(y))</code> instead of <code>conf_mat_prc = conf_mat/len(y)</code> to get float instead of 0 (int). </p></li>
</ul>

<p>Finally, for the second graph (line 48) use <code>str(round(conf_mat_prc[i, j]*100,precision)) + "%"</code> in which precision defines the number of floating point digits.</p>

<p>Here's the new code: </p>

<pre><code>import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
from sklearn.metrics import confusion_matrix

# %matplotlib inline

iris = datasets.load_iris()
x = iris['data']
y = iris['target']

x = x[:, :2]

clf_tree = DecisionTreeClassifier(random_state = 1)
fit_clf = clf_tree.fit(x, y)

y_pred_proba = fit_clf.predict_proba(x)
y_pred = fit_clf.predict(x)

conf_mat = confusion_matrix(y_true = y, y_pred = y_pred)

fig, ax = plt.subplots(figsize = (15, 9))
ax.matshow(conf_mat, cmap = plt.cm.Blues, alpha = 0.3)

for i in range(conf_mat.shape[0]):
    for j in range(conf_mat.shape[1]):
        ax.text(x = j, y = i,
               s = conf_mat[i, j],
               va = 'center',
                ha = 'center')

plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()



conf_mat_prc = conf_mat/float(len(y))

fig, ax = plt.subplots(figsize = (15, 9))
ax.matshow(conf_mat_prc, cmap = plt.cm.Blues, alpha = 0.3)


precision = 2
for i in range(conf_mat_prc.shape[0]):
    for j in range(conf_mat_prc.shape[1]):
        ax.text(x = j, y = i,
               s = str(round(conf_mat_prc[i, j]*100,precision)) + "%",
               va = 'center',
                ha = 'center')

plt.xlabel('Predicted % dist')
plt.ylabel('Actual % dist')
plt.show()
</code></pre>

<p>Here's the new second graph :</p>

<p><a href="https://i.stack.imgur.com/7CPku.png" rel="nofollow"><img src="https://i.stack.imgur.com/7CPku.png" alt="enter image description here"></a></p>

python numpy