Reputation: 1308
I have a matplotlib boxplot with very long strings as xticks. Is there a way to automatically split them into multiple lines to make the plot more clean? I am using the seaborn function barplot
to create the graph.
And the code i use to create it:
plt.figure(figsize=(15,13))
sns.barplot(x="Component",y="TTTR",color = "C0",data=new,dodge=False,edgecolor="black",zorder=3)
plt.xticks(rotation=90)
plt.grid(axis='y',zorder=0)
plt.title("10 most impactful components",size=30,y=1.04,**pfont)
plt.ylabel("Impact (Sum TTR in h)")
plt.xlabel('Component')
plt.tight_layout()
Upvotes: 2
Views: 3968
Reputation: 24430
seaborn.barplot
returns a matplotlib.Axes
object so you could use Axes.set_xticklabels
to update the labels afterwards, e.g.
import textwrap
max_width = 20
ax = sns.barplot(...)
ax.set_xticklabels(textwrap.fill(x.get_text(), max_width) for x in ax.get_xticklabels())
Upvotes: 9
Reputation: 1308
Found a workaround: Replacing the label column in the pandas dataframe with the labels from this answer
new.Component = [re.sub("(.{20})", "\\1\n", label, 0, re.DOTALL) for label in new.Component]
sns.barplot(x="Component",y="TTTR",color ="C0",data=new,dodge=False,edgecolor="black",zorder=3)
Upvotes: 0