1

I have this Series data type:

enter image description here

And I'm creating a bar using

gerarldine_count.plot.bar()
plt.xticks(wrap = False)

How do I wrap the text? All the explanations I've seen only show you when you create xticks labels manually, but in my case xticks labels come from the Series

enter image description here

Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • Welcome to SO. Please read [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and edit your question accordingly. Specifically, don't post data/code/error messages as images, post the text directly here on SO. If it is a pandas question, then please add the tag. – Mr. T Jan 09 '21 at 09:58

1 Answers1

3

You can solve this problem by introducing a library called 'textwrap'. Post your data as text, not images.

import pandas as pd
import numpy as np
import io
from textwrap import wrap
import matplotlib.pyplot as plt

data = '''
name value
"CHEMICAL AND TREATMENT PRODUCTS" 148
"NETWORK EQUIPMENTS AND METERS" 103
"PLANT AND PROCESS EQUIPMENTS AND MAINTENANCE" 54
"MEMBRANES AND MEMBRANES HOUSING" 23
"ELECTRICAL AND PROCESS CONTROL SYSTEMS" 16
"SLUDGE AND WASTE TRANSPORT AND TREATMENT" 6
"INSPECTIONS - ANALYSIS - TECHNICAL STUDIES" 3
"TECHNICAL SUPPLIES 1 ENERGY AND UTILITIES" 1
'''

df = pd.read_csv(io.StringIO(data), delim_whitespace=True)

labels = [ '\n'.join(wrap(l, 20)) for l in df.name]

ax = df.plot.bar()
ax.set_xticklabels(labels)

plt.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • 1
    Why didn't you mark it as a duplicate? At the very least, you could have [linked the answer](https://stackoverflow.com/a/47058132/8881141) from where you copied the code. – Mr. T Jan 09 '21 at 10:27