Seaborn:有没有更好的方法可以在条形图中包裹文本? [英] Seaborn: Is there a better way to wrap the text in my bar plot?

查看:35
本文介绍了Seaborn:有没有更好的方法可以在条形图中包裹文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为条形图编写函数,并且遇到了另一个小问题.我的ytick标签太长,导致无法看到我的y轴标签.只有大幅减少ytick标签的大小时,我才能看到y标签.

  def bar_plot(数据,x,y,标题):sns.set_style('darkgrid')数据= data.sort_values(升序=假,by = x)数据= data.head(n = 10)如果(data [x]> 1000000).any():数据[x] =数据[x]/1000000ax = sns.barplot(数据=数据,x = x,y = y)ax.set_title(title,size = 35)ax.set_xlabel(x +'($ Millions)',size = 15)ax.set_ylabel(y,size = 15)ax.set_yticklabels(data [y] .head(n = 10),wrap = True)别的:ax = sns.barplot(数据=数据,x = x,y = y)ax.set_xlabel(x,size = 15)ax.set_ylabel(y,size = 15)ax.set_title(title,size = 35)ax.set_yticklabels(data [y] .head(n = 10),wrap = True) 

我已经尝试过 ax.set_yticklabels(data [y] .head(n = 10),wrap = True)来包装文本.虽然有效,但是它不能包装足够的文字.有没有办法告诉 wrap = True 在x个字符后换行?我已经尝试使用Google搜索,但是找不到任何有效的方法.

修改

我正在使用的数据框的格式类似于

 客户名称Col 1 Col 2 Col 3 Col 4 Col 5某些名称51,235.00 nan 23,423.00 12,456.00 654.00公司名称较长152.00 5,626.00 nan 82,389.00 5,234.00名称12,554.00 5,850.00 1,510.00 nan 12,455.00公司12,464.00 nan 752.00 1,243.00 1,256.00公司长名12,434.00 78,915.00 522.00 2,451.00 6,567.00 

解决方案

正如

  ax.set_yticklabels([用于数据中[[Client Name]].head()的e的[textwrap.fill(e,7)))plt.show() 

将显示更类似的内容

I am writing a function for bar plots and have encountered another small problem. I have some ytick labels that are too long, causing my y axis label not able to be seen. I'm only able to see the y label when I drastically decrease the size of the ytick labels.

def bar_plot(data, x, y, title):
    sns.set_style('darkgrid')
    data = data.sort_values(ascending=False, by=x)
    data = data.head(n=10)
    if (data[x]>1000000).any():
        data[x] = data[x] / 1000000
        ax = sns.barplot(data=data, x=x, y=y)
        ax.set_title(title, size=35)
        ax.set_xlabel(x + ' ($ Millions)', size=15)
        ax.set_ylabel(y, size=15)
        ax.set_yticklabels(data[y].head(n=10), wrap=True)

    else:
       ax = sns.barplot(data=data, x=x, y=y)
       ax.set_xlabel(x, size=15)
       ax.set_ylabel(y, size=15)
       ax.set_title(title, size=35)
       ax.set_yticklabels(data[y].head(n=10), wrap=True)

I have tried ax.set_yticklabels(data[y].head(n=10), wrap=True) to wrap the text. While it works, it doesn't wrap the text enough. Is there a way to tell wrap=True to wrap after x amount of characters? I've tried googling this but couldn't quite find anything that works.

Edit

The format of the dataframe that I am working with is similar to

Client Name            Col 1      Col 2      Col 3      Col 4       Col 5
Some name              51,235.00  nan        23,423.00  12,456.00   654.00
Some long company name 152.00     5,626.00   nan        82,389.00   5,234.00
Name                   12,554.00  5,850.00   1,510.00   nan         12,455.00
Company                12,464.00  nan        752.00     1,243.00    1,256.00
Long Company Name      12,434.00  78,915.00  522.00     2,451.00    6,567.00

解决方案

As @ImportanceOfBeingErnest pointed out, you can use the textwrap module to do this, specifically useful would be textwrap.fill():

textwrap.fill(text[, width[, ...]])

Wraps the single paragraph in text so every line is at most width characters long, and returns a single string containing the wrapped paragraph. fill() is shorthand for

"\n".join(wrap(text, ...))

Although you will need to call this on each label separately with something like

ax.set_yticklabels([textwrap.fill(data[y].head(n=10)[i], width) for i in range(10)])


Edit

Here is a more complete example to show the usage:

import textwrap
import matplotlib.pyplot as plt
import pandas as pd

df = {'Client Name': ['Some Name', 'Some long company name', 'Name',
            'Company', 'Long Comany Name'],
      'Col 1': [51235, 152, 12554, 12464, 12434]}
data = pd.DataFrame(df)

fig, ax = plt.subplots(1)


ax.set_yticklabels(data['Client Name'].head())
plt.show()

This will show the following

whereas

ax.set_yticklabels([textwrap.fill(e, 7) for e in data['Client Name'].head()])
plt.show()

will show something more like

这篇关于Seaborn:有没有更好的方法可以在条形图中包裹文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆