Reputation: 343
This code generates individual boxplots for samples subdivided into three groups of data. So, dataset 1 has samples A, B and C and so on. When I plot the x-axis I only want the sample names ('Sample A, 'Sample B' etc) and NOT the major labels (i.e. 'dataset1', 'dataset2' etc) - I don't know how to suppress the major labels.
import matplotlib.pyplot as plt
import numpy as np
import random
#build dataset as dictionary
data = {}
data['dataset1'] = {}
data['dataset2'] = {}
data['dataset3'] = {}
#simulate data
n = 100
for k,v in data.iteritems():
upper = random.randint(0, 1000)
v['sample A'] = np.random.uniform(0, upper, size=n)
v['sample B'] = np.random.uniform(0, upper, size=n)
v['sample C'] = np.random.uniform(0, upper, size=n)
fig, axes = plt.subplots(ncols=3, sharey=True)
fig.subplots_adjust(wspace=0)
#build subplots
for ax, name in zip(axes, ['dataset1', 'dataset2', 'dataset3']):
ax.boxplot([data[name][item] for item in ['sample A', 'sample B', 'sample C']])
ax.set(xticklabels=['sample A', 'sample B', 'sample C'], xlabel=name)
ax.margins(0.05) # Optional
#plot labels
for ax in fig.axes:
plt.sca(ax)
plt.xticks(ha = 'right', rotation=45)
plt.show()
In case your wondering this is causing a major presentation problem for my actual dataset.
Upvotes: 0
Views: 125
Reputation: 39042
Both the other solutions first created the x-labels and then removed/hid them. The simplest way is to not put the labels in the first place.
Just use
ax.set(xticklabels=['sample A', 'sample B', 'sample C'])
without passing the argument xlabel=name
Upvotes: 2
Reputation: 3711
Set the labels invisible
#build subplots
for ax, name in zip(axes, ['dataset1', 'dataset2', 'dataset3']):
ax.boxplot([data[name][item] for item in ['sample A', 'sample B', 'sample C']])
ax.set(xticklabels=['sample A', 'sample B', 'sample C'], xlabel=name)
ax.margins(0.05) # Optional
ax.xaxis.label.set_visible(False) # Add this to suppress the major labels
Upvotes: 1
Reputation: 7331
An easy solution replace the label with a empty string, or make it invisible:
for ax in fig.axes:
plt.xlabel('')
# or
# ax.xaxis.label.set_visible(False)
Upvotes: 1