Rename a variable in the plot after transformation


I have a pandas dataframe “df” with a few columns, one of which is “a”. That is, we have a column in the dataframe:


With some transformations on the variable, we define two new variables

b = f(df['a'])
d = g(df['a'])

Presenting the two variables in the graph

import matplotlib.pyplot as plt

The legends for both variables are ‘a’, but I expect ‘b’ and ‘d’.
How can I make the legend ‘b’ and ‘d’ instead of ‘a’?

Why do you expect ‘b’ and ‘d’? Those are only the names of the variables; it’s the name of the column that’s the source of the legend.

Try this:

b = pd.DataFrame({'b': f(df['a'])})
d = pd.DataFrame({'d': g(df['a'])})
1 Like