I have 20 column and I perform univariate analysis my plots are generated as expected but the don’t have top bar value, here my code work perfectly but without top bar value for each plot.
fig, axes = plt.subplots(figsize=(15,12), ncols=5, nrows=4)
for col, ax in zip(df.columns, np.ravel(axes)):
df[col].value_counts().plot(kind='bar', ax = ax, rot=0, title=col)
plt.tight_layout()
plt.show()
>Solution :
You can use annotate for this problem. Here is the sample:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create a sample dataframe (replace with your actual dataframe)
data = {
'Column1': np.random.choice(['A', 'B', 'C', 'D'], 100),
'Column2': np.random.choice(['X', 'Y', 'Z'], 100),
}
df = pd.DataFrame(data)
fig, axes = plt.subplots(figsize=(15, 12), ncols=5, nrows=4)
for col, ax in zip(df.columns, np.ravel(axes)):
value_counts = df[col].value_counts()
ax = value_counts.plot(kind='bar', ax=ax, rot=0, title=col)
for p in ax.patches:
ax.annotate(f'{p.get_height()}', (p.get_x() + p.get_width() / 2., p.get_height()), ha='center', va='center')
plt.tight_layout()
plt.show()
The output will be like this :
When you run, maybe you will have different output since I just put random when create dataframe.
