I have a plot with both Major and Minor Ticks. It is a grouped bar chart and I don't want the legend describing each category, but want each category shown in the minor ticks. Then I want each group to have the name in the major ticks.
I can do this to some extent, but the major tick seems to override the minor tick at the same location. How can I turn this off?
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(
{
0: [84, 41],
1: [0, 0],
2: [18.5, 3.5],
3: [65.5, 124],
4: [0.5, 0]
},
index = ['A', 'B']
)
ax = df.plot.bar(
rot=0,
legend=False,
color=['gray', 'red', 'green', 'lightblue', 'darkblue',],
)
labels = df.unstack().index
label_x, label_mode = [], []
for rect, idx in zip(ax.patches, labels):
print(rect, idx)
label_mode.append(idx[0])
label_x.append(rect.xy[0] + (rect.get_width() * 0.5))
ax.set_xticklabels(['A', 'B'])
ax.set_xticks(label_x, minor=True)
ax.set_xticklabels(label_mode, minor=True)
ax.tick_params(axis='x', which='minor', length=0)
ax.tick_params(axis='x', which='major', length=10, width=0)
This was actually very simple. It is a single parameter remove_overlapping_locs
passed to xaxis
object.
ax.xaxis.remove_overlapping_locs = False
See matplotlib API changes 3.1.0 for more details.