'Getting multiple legends from for loop with specific data column and row as legend title

I have a plot that iterates through different databases in a list. However, I of course want to know which database is what graph. The databases don't have a title. So I was thinking of using a specific point from a column / row. Which makes it clear for me what database I am seeing.

Inorder to extract a specific float from a column / row which is contained in my list of databases,

I use: specific_float = list_of_databases[list_index][database_column_name][database_index]

The same principle I would like to use for my for loop with plots. So I was thinking of something like this:

Some context about the names in the code:

  • dc_oppervlakte_filter is the list with dataframes
  • TP_TARGET is an column name from the dataframes
  • TP_SMOTE same story as TP_TARGET
  • STRIP_WIDTH is the column I want to use for my legend with index 0
for k in dc_oppervlakte_filter:
    plt.plot(k['TP_TARGET'], k['TP_SMOTE'], 'o', label = k['STRIP_WIDTH'][0])
plt.xlabel("TP_TARGET in ($\u00b0C$)")
plt.ylabel("TP_SMOTE in ($\u00b0C$)")
plt.legend(loc = 'lower right')

However, this gives me the error: Keyerror: 0

So I tried:

for k in dc_oppervlakte_filter:
    plt.plot(k['TP_TARGET'], k['TP_SMOTE'], 'o', label = dc_oppervlakte_filter[k]['STRIP_WIDTH'][0])
plt.xlabel("TP_TARGET in ($\u00b0C$)")
plt.ylabel("TP_SMOTE in ($\u00b0C$)")
plt.legend(loc = 'lower right')

But it gives me the error: TypeError: list indices must be integers or slices, not DataFrame

An optimal scenario for me would look like that there's an legend with the specific float chosen from every database that's used to plot a graph.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source