0

Python's textwrap function isn't working for me in Seaborn: https://docs.python.org/3/library/textwrap.html

I followed this tutorial to wrap the text on my xtick labels: https://medium.com/dunder-data/automatically-wrap-graph-labels-in-matplotlib-and-seaborn-a48740bc9ce

Input from tutorial

Output from tutorial

However when I run my code, I get no error message and my x-axis ticks are still overlapping. I pasted relevant parts of my code below. Any help would be greatly appreciated!

########### Libraries used for my code

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from openpyxl.workbook import Workbook
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
import xlwings as xw
import textwrap
from textwrap import wrap

########### Defining bar plot parameters

fig, ax = plt.subplots (figsize= (10.95,4.23))
sns.set_style('whitegrid')

ax = sns.barplot(data=data, x='Kinase Target', 
y=ws['C2'].value,width=1,hue='Group')

########### Format x-axis based on number of rows

rows_count = data.shape[0]


if 10 > rows_count >= 0 :
    plt.xticks(rotation =45,horizontalalignment='right', 
fontweight='bold', fontsize = 12)
plt.subplots_adjust(top=0.85, bottom=0.8, left=0.073, right=0.852)

########### I want to wrap xtick label text as part of my first if statement

def wrap_labels(ax, width, break_long_words=False):
        labels = []
     for label in ax.get_xticklabels():
        text = label.get_text()
        labels.append(textwrap.fill(text, width, break_long_words=break_long_words))
        ax.set_xticklabels(labels, rotation=0)


        wrap_labels(ax, 0.5)
        ax.figure

########### Next if statement (I go on to format other parts of the graph like title and y ticks)

elif 20 > rows_count >= 10 :
plt.xticks(rotation = 45,horizontalalignment='right', fontweight='bold', fontsize = 10)
Moo
  • 1
  • 1
  • Is this in Jupyter? – Trenton McKinney Mar 08 '23 at 00:01
  • See https://stackoverflow.com/a/61394688/7758804 – Trenton McKinney Mar 08 '23 at 00:05
  • @TrentonMcKinney I'm using Python in Visual Studio Code. The post you linked is more about the graph title rather than the xticks. However, I can try to apply it to my code tomorrow. Why do you think my current code giving me the desired result or at least an error value of some sort? Could it have something to do with how I defined ax or how I placed the for loop in the if statement? – Moo Mar 08 '23 at 03:12
  • Sorry, I misunderstood the question. – Trenton McKinney Mar 08 '23 at 03:52
  • However, if you are working in interactive mode with Jupyter in VS code, the same issue “may” apply. You could replace the space in the tick labels with a newline `\n` – Trenton McKinney Mar 08 '23 at 03:55
  • If your code is one long py file that you’re executing, that’s different environment than if your code is broken into different cells. If the second case is true, all of the code for `ax` must be in the same cell. – Trenton McKinney Mar 08 '23 at 04:04
  • Also, the code examples appear to not be correctly indented. – Trenton McKinney Mar 08 '23 at 04:06

0 Answers0