How to center align the plot over the xticks
Question:
My boxplot seem not align with the x-tick of the plot. How to make the boxplot align with the x-tick?
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame([['0', 0.3],['1', 0.5],['2', 0.9],
['0', 0.8],['1', 0.3],['2', 0.4],
['0', 0.4],['1', 0.0],['2', 0.7]])
df.columns = ['label', 'score']
label_list = ['0', '1', '2']
fig = plt.figure(figsize=(8, 5))
g=sns.boxplot(x='label', y='score', data=df, hue='label', hue_order=label_list)
g.legend_.remove()
plt.show()
Answers:
You can add dodge=False
into your boxplot
line & that should fix this.
That updated code would be as below
g=sns.boxplot(x='label', y='score', data=df, hue='label', hue_order=label_list, dodge=False)
You can then play with width
to control the width (default width is 0.8) of box plots like below
g=sns.boxplot(x='label', y='score', data=df, hue='label', hue_order=label_list, dodge=False, width=.2)
- As stated by ImportanceOfBeingErnest, remove
hue='label', hue_order=label_list
. That is responsible for shifting the bars.
- The purpose of
hue
is to encode a different category of variables, not to encode the same category, 'label'
, multiple times.
- The
'label'
category is already encoded on the x-axis. Likewise, there doesn’t need to be a legend, because the information is already encoded on the x-axis.
- While some packages, like
ggplot
, and seaborn
, may encode every category on the x-axis with a color, it’s better to avoid unnecessary usage of color.
- From Practical Rules for Using Color in Charts by Stephen Few, Perceptual Edge, in the Use Color Meaningfully and with Restraint section: the color usage in this plot, means nothing, and should be avoided.
- Also see Graphical Perception and Graphical Methods for Analyzing Scientific Data, William S. Cleveland and Robert McGill, New Series, Vol. 229, No. 4716 (Aug. 30, 1985), pp. 828-833 (6 pages), published by: American Association for the Advancement of Science
ax = sns.boxplot(data=df, x='label', y='score')
ax.set(title='Default Plot: Unnecessary Usage of Color')
ax = sns.boxplot(data=df, x='label', y='score', color='tab:blue')
ax.set(title='Avoids Unnecessary Usage of Color')
- If the order of categories on the x-axis is important, and not correctly ordered, one of the following options can be implemented:
- Specify the order with the
order
parameter.
order=['0', '1', '2']
or order=label_list
- Convert the
df
column to a category Dtype
with pd.Categorical
df.label = pd.Categorical(values=df.label, categories=label_list, ordered=True)
My boxplot seem not align with the x-tick of the plot. How to make the boxplot align with the x-tick?
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame([['0', 0.3],['1', 0.5],['2', 0.9],
['0', 0.8],['1', 0.3],['2', 0.4],
['0', 0.4],['1', 0.0],['2', 0.7]])
df.columns = ['label', 'score']
label_list = ['0', '1', '2']
fig = plt.figure(figsize=(8, 5))
g=sns.boxplot(x='label', y='score', data=df, hue='label', hue_order=label_list)
g.legend_.remove()
plt.show()
You can add dodge=False
into your boxplot
line & that should fix this.
That updated code would be as below
g=sns.boxplot(x='label', y='score', data=df, hue='label', hue_order=label_list, dodge=False)
You can then play with width
to control the width (default width is 0.8) of box plots like below
g=sns.boxplot(x='label', y='score', data=df, hue='label', hue_order=label_list, dodge=False, width=.2)
- As stated by ImportanceOfBeingErnest, remove
hue='label', hue_order=label_list
. That is responsible for shifting the bars. - The purpose of
hue
is to encode a different category of variables, not to encode the same category,'label'
, multiple times.- The
'label'
category is already encoded on the x-axis. Likewise, there doesn’t need to be a legend, because the information is already encoded on the x-axis. - While some packages, like
ggplot
, andseaborn
, may encode every category on the x-axis with a color, it’s better to avoid unnecessary usage of color. - From Practical Rules for Using Color in Charts by Stephen Few, Perceptual Edge, in the Use Color Meaningfully and with Restraint section: the color usage in this plot, means nothing, and should be avoided.
- Also see Graphical Perception and Graphical Methods for Analyzing Scientific Data, William S. Cleveland and Robert McGill, New Series, Vol. 229, No. 4716 (Aug. 30, 1985), pp. 828-833 (6 pages), published by: American Association for the Advancement of Science
- The
ax = sns.boxplot(data=df, x='label', y='score')
ax.set(title='Default Plot: Unnecessary Usage of Color')
ax = sns.boxplot(data=df, x='label', y='score', color='tab:blue')
ax.set(title='Avoids Unnecessary Usage of Color')
- If the order of categories on the x-axis is important, and not correctly ordered, one of the following options can be implemented:
- Specify the order with the
order
parameter.order=['0', '1', '2']
ororder=label_list
- Convert the
df
column to acategory Dtype
withpd.Categorical
df.label = pd.Categorical(values=df.label, categories=label_list, ordered=True)
- Specify the order with the