Home > Enterprise >  Annotation not happening in matplotlib
Annotation not happening in matplotlib

Time:12-15

I have data like this

|               | FA1  | FA2  | FA3  |
|SS Loadings    | 1.6  | 1.3  | 0.58 |
|Proportion Var | 0.27 | 0.20 | 0.10 |

I am trying to graph it up and this is the code I am using

import matplotlib.pyplot as plt

plt.scatter(data.T.index.values, data.T['Proportion Var'])
plt.plot(data.T.index.values, data.T['Proportion Var'])

for i, txt in enumerate(data.T['Proportion Var']):
    ax.annotate(txt, (data.T.index.values[i], data.T['Proportion Var'][i]))

plt.title('Proportional Var')
plt.xlabel('Factors')
plt.ylabel('Variance explained')
plt.grid()
plt.show()

The graph my python is plotting

This graph has no annotation. I looked through several questions to see if I am doing something wrong and I have not been able to catch it. Any help is appreciated!

CodePudding user response:

Your variable ax is undefined. You should have seen an error message. If you want to use annotations, you must define it:

fig, ax = plt.subplots()
ax.scatter(data.T.index.values, data.T['Proportion Var'])
ax.plot(data.T.index.values, data.T['Proportion Var'])

for i, txt in enumerate(data.T['Proportion Var']):
    ax.annotate(txt, (data.T.index.values[i], data.T['Proportion Var'][i]))

ax.set_title('Proportional Var')
ax.set_xlabel('Factors')
ax.set_ylabel('Variance explained')
plt.grid()
plt.show()
  • Related