How do I get different x-axis labels in a subplot when generating it inside a for loop, as such;
attributes = ["attr_with_2_categories", "attr_with_5_categories"]
target = 'Target'
for idx, variable in enumerate(attributes):
plt.subplot(2, 1, idx+1)
df_rate = DataSet[[target,variable]].groupby([variable]).mean()
counts = DataSet[variable].value_counts()
output = pd.concat((df_rate, counts), axis=1, sort=False)
output.columns = ["DR", "Counts"]
labels = np.unique(DataSet[variable])
ax1 = output["Counts"].plot(kind='bar', width=0.5, color='skyblue', use_index=True)
plt.ylabel("Count")
ax1.set_xticklabels(labels, rotation = 45)
ax2 = plt.twinx(ax1)
ax2.plot(ax1.get_xticks(),output["DR"], linestyle='-', marker='o', linewidth=3.0)
plt.ylabel("DR")
plt.grid(False)
plt.tight_layout()
plt.show()
The result here is two graphs but both graphs end up with the labels of the last graph that has 5 labels wile the first graph has only 2.