0

I have created figure with matplotlib:

enter image description here and I would like to add each point tag but this data should be retrieved from a column ,so each point gets tag based on the "name" column in the dataframe.

enter image description here

Is there any way to add the data based on the column?

I have try to add it with this:

plt.text(1, 1, df['name'], fontsize=9)

but it didn't work and I got this error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

this is how I built the whole graph:

df_PCA=pca_func(StandardNormalVariate(df_time.iloc[:,15:]))

#PLOT 
fig = plt.figure(figsize = (12,8))
ax = fig.add_subplot(1,1,1) 
ax.set_xlabel('PCA1', fontsize = 15)
ax.set_ylabel('PCA2', fontsize = 15)
ax.set_title('PCA - 6 Treatments- 21/06/2019 14:00', fontsize = 20)
#plt.text(1, 1, df_time['name'], fontsize=9)

targets = df_time.code.unique()
colors = ['tab:red','tab:green','tab:blue','tab:orange','tab:purple','tab:cyan']
for target, color in zip(targets,colors):
    #goes to line. and check of 1,2,3,4,5,6 are true or false, 6 times total, and if true gives the color.
    indicesToKeep = df['code'] == target
    ax.scatter(df_PCA.loc[indicesToKeep, 'principal component 1']
               , df_PCA.loc[indicesToKeep, 'principal component 2']
               , c = color
               , s = 100)
ax.legend(targets)
ax.grid()

My end goal: to add the names of observation beside each point in order to identify the top-right point and to rmove it from my dataset

Reut
  • 1,555
  • 4
  • 23
  • 55
  • `plt.text` wants only one string to place at an x,y position. It can not handle a complete Series of strings, and also wouldn't know where to place all that text. Clearly putting everything on the same position wouldn't be OK for nobody. Even just plotting these at the same place as the scatter dot would create a messy sea of numbers. Maybe something such as [mplcursors](https://mplcursors.readthedocs.io/en/stable/) would be convenient for your use case? – JohanC Feb 12 '20 at 14:01
  • consider looking into bokeh Hover feature to add interactive information to points [bokeh](https://bokeh.org/) – GLarose Feb 12 '20 at 14:11
  • Solutions such as bokeh are certainly worth studying. Bokeh makes use of an interactive web interface. If you want to stay inside the matplotlib ecosystem with mplcursors, [here](https://stackoverflow.com/questions/7908636/possible-to-make-labels-appear-when-hovering-over-a-point-in-matplotlib) and [here](https://stackoverflow.com/questions/59800059/how-to-use-two-mplcursors-simultaneously-for-a-scatter-plot-of-two-sets) are some examples with scatter plots. – JohanC Feb 12 '20 at 14:33
  • to be honest, my goal in tag the data was to indentify the point in the right-top side of the screen and to remove it from the dataset – Reut Feb 12 '20 at 14:37

0 Answers0