The following code only shows the main category ['one', 'two', 'three', 'four', 'five', 'six'] as the x axis labels. Is there a way show subcategory ['A', 'B', 'C', 'D'] as secondary x axis labels?
df = pd.DataFrame(np.random.rand(6, 4),
index=['one', 'two', 'three', 'four', 'five', 'six'],
columns=pd.Index(['A', 'B', 'C', 'D'],
name='Genus')).round(2)
df.plot(kind='bar',figsize=(10,4))
On the Layout tab, in the Labels group, click Data Labels, and then click the option that you want. For additional data label options, click More Data Label Options, click Label Options if it's not selected, and then select the options that you want.
Labels of the bar charts are listed below: The horizontal axis (x-axis) The vertical axis (y-axis) Title of the bar chart.
How to plot a Bar Chart with multiple labels in Matplotlib? Make some data set for men_means, men_std, women_means, and women_std. Make index data points using numpy. Initialize the width of the bars. Use subplots () method to create a figure and a set of subplots. Create rects1 and rects2 bars rectangle using bar () method.
plt.ylabel () method plots labels on the y-axis and we pass the fontsize parameter and set its value to 20. Here we learn how to add a text label in the specific bar of the bar chart. Firstly you have to define data and plot the bar by using the plt.bar () method.
Insert two blank rows before each main subcategory (except the first one) in the data range. Then you can see categories and data bars in the chart are separated with blank spaces as well. 7.
Select the data range, click Insert > Insert Column or Bar Chart > Clustered Bar. 3. Drag the chart border to enlarge the chart area. See the below demo. 4. Right click the bar and select Format Data Series from the right-clicking menu to open the Format Data Series pane.
Here is a solution. You can get the positions of the bars and set some minor xticklabels accordingly.
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.rand(6, 4),
index=['one', 'two', 'three', 'four', 'five', 'six'],
columns=pd.Index(['A', 'B', 'C', 'D'],
name='Genus')).round(2)
df.plot(kind='bar',figsize=(10,4))
ax = plt.gca()
pos = []
for bar in ax.patches:
pos.append(bar.get_x()+bar.get_width()/2.)
ax.set_xticks(pos,minor=True)
lab = []
for i in range(len(pos)):
l = df.columns.values[i//len(df.index.values)]
lab.append(l)
ax.set_xticklabels(lab,minor=True)
ax.tick_params(axis='x', which='major', pad=15, size=0)
plt.setp(ax.get_xticklabels(), rotation=0)
plt.show()
Here a possible solution (I had quite a lot of fun!):
df = pd.DataFrame(np.random.rand(6, 4),
index=['one', 'two', 'three', 'four', 'five', 'six'],
columns=pd.Index(['A', 'B', 'C', 'D'],
name='Genus')).round(2)
ax = df.plot(kind='bar',figsize=(10,4), rot = 0)
# "Activate" minor ticks
ax.minorticks_on()
# Get location of the center of each rectangle
rects_locs = map(lambda x: x.get_x() +x.get_width()/2., ax.patches)
# Set minor ticks there
ax.set_xticks(rects_locs, minor = True)
# Labels for the rectangles
new_ticks = reduce(lambda x, y: x + y, map(lambda x: [x] * df.shape[0], df.columns.tolist()))
# Set the labels
from matplotlib import ticker
ax.xaxis.set_minor_formatter(ticker.FixedFormatter(new_ticks)) #add the custom ticks
# Move the category label further from x-axis
ax.tick_params(axis='x', which='major', pad=15)
# Remove minor ticks where not necessary
ax.tick_params(axis='x',which='both', top='off')
ax.tick_params(axis='y',which='both', left='off', right = 'off')
Here's what I get:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With