I am trying to create a plot
pd.crosstab(df['cardio'], df['cholesterol']).plot(kind = 'bar')
plt.xlabel('0 = No Heart Disease, 1 = Heart Disease Present')
plt.ylabel('Number of people')
#####plt.legend(['Above Normal','High', 'Normal'])
plt.legend(df['cholesterol'])
plt.title("Distribution w.r.t cholesterol")
plt.show()
The dataframe has cholesterol values as 1, 2, 3 for normal, above normal, well above normal respectively. The above code gives legend with 1, 2, 3. Is there any way to replace the legend value with a custom string, that is, I can define that 1 should be displayed as "normal" in legend, 2 as "above normal" and 3 as "well above normal". Thanks in advance.
CodePudding user response:
The data was created independently using the information in the comments. I also aggregated the pandas plots since you can set titles and labels for the axes. You can then get the handles and labels from the created legend, and specify the labels you want to replace.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import random
df = pd.DataFrame({'cardio': random.choices([0,1], k=100),
'cholesterol': random.choices([1,2,3], k=100)})
ax = pd.crosstab(df['cardio'], df['cholesterol']).plot(kind = 'bar',
title='Distribution w.r.t cholesterol',
xlabel='0 = No Heart Disease, 1 = Heart Disease Present',
ylabel='Number of people'
)
new_labels = ['Above Normal','High', 'Normal']
handler, _ = ax.get_legend_handles_labels()
ax.legend(handler, new_labels, loc='upper center', title='cholesterol')