How can I increase the space between two specefic boxes in a seaborn boxplot? In the tips dataset, how can i modify the spacing between Sat and Sun without affecting the other boxes. I already included empty columns in the dataframe but with this workaround it's not possible to controll the spacing.
%matplotlib inline
import seaborn as sns
tips = sns.load_dataset("tips")
ax = sns.boxplot(x="day", y="total_bill", data=tips)
Seaborn Box Plots with Multiple Columns You can plot multiple box plots depending on the number of unique values in the categorical column for which you want to draw your box plot. The categorical column name is passed to the x variable while the numeric column name should be passed to the y variable.
In seaborn, the hue parameter determines which column in the data frame should be used for colour encoding. Using the official document for lmplot provided an example for this.
Steps. Set the figure size and adjust the padding between and around the subplots. Make a Pandas dataframe, i.e., two-dimensional, size-mutable, potentially heterogeneous tabular data. Make a box and whisker plot, using boxplot() method with width tuple to adjust the box in boxplot.
To my knowledge, this is not possible with seaborn because it sadly does not provide any means of modifying the positions
keyword. See also this similar question.
The easiest workaround is to use a different boxplot function, for example the one that comes with pandas dataframes:
bplot = tips.boxplot(by="day", column="total_bill", positions=[1,2,3,4.5])
Of course, this is nowhere near as nicely styled as the seaborn version.
Fortunately, matplotlib provides infinite options to those willing to explore them, so one could create something similar to the seaborn plot by modifying the different parts of the plot accordingly.
This comes close:
# Prep
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset("tips")
# Create boxplot
bplot = tips.boxplot(by="day", column="total_bill", positions=[1,2,3,4.5],
return_type='dict', figsize=(8,6), grid=False, patch_artist=True,
sym='d', fontsize=16)
# Style boxplot
colors = ['blue', 'green', 'red', 'cyan']
for patch, color in zip(bplot['total_bill']['boxes'], colors):
patch.set_facecolor(color)
patch.set_edgecolor('0.2')
patch.set_linewidth(1.5)
for whisker in bplot['total_bill']['whiskers']:
whisker.set_color('0.2')
whisker.set_linewidth(1.5)
for fliers in bplot['total_bill']['fliers']:
fliers.set_markerfacecolor('0.2')
for median in bplot['total_bill']['medians']:
median.set_color('0.2')
median.set_linewidth(1.5)
for caps in bplot['total_bill']['caps']:
caps.set_color('0.2')
caps.set_linewidth(1.5)
# Other adjustments
plt.title("")
plt.suptitle("")
plt.xlabel("day", fontsize=18)
plt.ylabel("total_bill", fontsize=18)
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