I am trying to add a title to my plot but it's not working.
data.hist(by=['ascites', 'sex', 'drug'])
plt.title('Ascites Distribution')
plt.xlabel('Age')
plt.ylabel('Number of Patients')
plt.show()
The title and xlabel are not showing.
THis is how to do this correctly:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
np.random.seed(0)
data = pd.DataFrame({
'ascites': np.random.choice(['Yes', 'No'], size=100),
'sex': np.random.choice(['Male', 'Female'], size=100),
'drug': np.random.choice(['Drug1', 'Drug2'], size=100),
'Age': np.random.randint(20, 60, size=100)
})
axes = data.hist(by=['ascites', 'sex', 'drug'], figsize=(10, 8), layout=(4, 2), xrot=45)
plt.gcf().suptitle('Ascites Distribution', fontsize=16, y=1.02)
for ax in axes.flatten():
ax.set_xlabel('Age', fontsize=12)
ax.set_ylabel('Number of Patients', fontsize=12)
plt.tight_layout()
plt.show()