I'm trying to write a function that generates a barplot given a PD column, and a list of labels for the legend. I have categorical data that's been numerically encoded, so the default labels are just numbers that I want to change back to string values. Here's my code:
legend_labels = ["label 1", "label 2"]
def plot_percentages(col, legend_labels):
plt.figure(figsize=(7,6))
ax = sns.barplot(x="x", y="x", hue=col, data=df,
estimator=lambda x: len(x) / len(df) * 100)
ax.set(ylabel="Percentage")
plt.xticks([0, 1], ['A', "B"])
L=plt.legend()
for idx, val in pd.DataFrame(legend_labels).iterrows():
L.get_texts()[idx].set_text(val)
ax
It returns the following error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Thanks in advance for any suggestions on how to fix this.
The solution is to not use iterows()
here but rather just a simple for loop.
for idx, val in enumerate(legend_labels):
L.get_texts()[idx].set_text(val)