0

I have a dictionary of data that looks like this:

dict_df

dict_df.keys()
# dict_keys(['Facebook', 'Amazon', 'Google', 'Apple'])
dict_df.values() 

dict_values([group
below 10        9.50
above 10        3.49
above 20        1.37
above 40        3.78
Name: mix, dtype: float64, group
below 10        9.62
above 10        3.83
above 20        1.51
above 40        3.78
Name: mix, dtype: float64, group
below 10        9.62
above 10        3.83
above 20        1.51
above 40        3.78
Name: mix, dtype: float64, group
below 10        9.62
above 10        3.83
above 20        1.51
above 40        3.78

I draw multiple graphs like so:

for i,plot in enumerate(dict_df.keys()):
    plt.figure(i)
    ax = sns.barplot(y = 'group', x = 'mix', 
                    data = dict_df[plot].reset_index(), 
                    orient = 'h').set_title(plot)


    plt.savefig(rf'Folder\{plot}')

Which draws a bar chart for every key in my dict_df, now I want to add labels to the end of the vertical bars, I read many solutions but none of them seem to work.

I tried adding this inside my for loop that draws the graphs:

for i in range(len(dict_df[plot])):
        plt.annotate(str(dict_df[plot].keys()[i]), xy=(dict_df[plot].keys()[i],
                         dict_df[plot].values()[i]), ha='center', va='bottom')

Which I found on this answer But I get TypeError: 'numpy.ndarray' object is not callable error.

Jonas Palačionis
  • 4,591
  • 4
  • 22
  • 55
  • From what I see, it should work. We need a minimal reproducible example to investigate further. Are you sure the `TypeError` is located within the for loop ? – Liris Oct 05 '21 at 11:41
  • @Liris, managed to fix that error by removing `()` from the `dict_df[plot].values()[i]` as its a pandas dataframe so values are returned without having the `()` but now I get `ConversionError: Failed to convert value(s) to axis units: 'Considered Co-ordinators'`, `ValueError: Missing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric data` – Jonas Palačionis Oct 05 '21 at 12:07

1 Answers1

0

I was able to solve it using:

 for i in range(len(dict_df[plot])):
        plt.annotate((round(dict_df[plot].values[i],2)), 
        xy=(dict_df[plot].values[i], i), 
        ha='right', va='bottom')

Instead of using dict_df[plot].keys()[i], maybe there might be an error due to decoding as it throws

ConversionError: Failed to convert value(s) to axis units: 'Considered Co-ordinators'

When using names of the columns instead of the index of it.

Jonas Palačionis
  • 4,591
  • 4
  • 22
  • 55