You can change the bar width in Seaborn with the following code. The given example helps you to understand how to change the bar width of the Seaborn plot. I highly recommend you “Python Crash Course Book” to learn Python.
Example: Change the Bar Width in Seaborn
Python
x
# Import the required libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Load the Dataset
df = sns.load_dataset("titanic")
fig, ax = plt.subplots()
# Create Barplot
sns.barplot(data=df, ax=ax, x="class", y="survived", hue="sex")
# Define Function in which we will pass new width
def Width(ax, new_width) :
for patch in ax.patches :
current_width = patch.get_width()
difference = current_width - new_width
# Set new width
patch.set_width(new_width)
# Now Recenter the Bars
patch.set_x(patch.get_x() + difference * .5)
# You can change width i.e. 0.5 for this example
Width(ax, 0.5)
# Display the plot
plt.show()
Output:
