在海运条形图x轴Python中换行文本 [英] Wrap text in seaborn barplot x-axis python

查看:19
本文介绍了在海运条形图x轴Python中换行文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制数据帧,并且我希望x轴标签不会像这里那样相互覆盖

处理一个曲线图时,添加.set_xticklabels(rotation=30)确实解决了这个问题:

sns.catplot("foo", data=boo, aspect=5, kind="count" ).set_xticklabels(rotation=30)

但是当我添加ax参数时,如下所示:

sns.catplot("foo", data=boo, aspect=5, kind="count" , ax=fig.add_subplot(211) ),

不是(请参阅第一个快照)。

我已尝试sns.catplot("foo", data=boo, aspect=5, kind="count", ax=ax1).set_xticklabels(rotation=30)

我正在寻找某种Excel的"换行文本"。我试着在海运属性中寻找,但没有成功。我还尝试将x轴值设置得更小,但它仍然覆盖了某些标签中的每个值。

推荐答案

光盘-这是我在‘StackOF’上的第一篇帖子。如果没有达到您的期望,请原谅。在海运中包装x轴标签是不可能的(AFAIK),但即使使用DEXPT,也有太多限制。以下是使用Matplotlib(使用Python)实现此目标的逻辑。

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

raw_data = {'plan_type':  ['this is big ticks number one', 'this is also a big ticks number two Group B', 'and this as well a very big ticks number three Group C', 'this is no exception than a very big ticks number four Group D','Group E'],
        'Total': [56,24,18,49,67],
        'Total_PY': [12,82,33,60,16]
}


df = pd.DataFrame(raw_data, 
              columns = ['plan_type', 'Total', 'Total_PY'])


fig, ax = plt.subplots(figsize=(14,9))
ax = df.plot.bar(rot=0, ax=ax, color=['#FF5733','#2323ff'], 
             width = 0.7 )


for p in ax.patches[1:]:
h = p.get_height()
x = p.get_x()+p.get_width()/2.
if h != 0 :
    ax.annotate("%g" % p.get_height(), xy=(x,h), xytext=(0,4), rotation=0, 
               textcoords="offset points", ha="center", va="bottom")


ax.set_xlim(-0.5, None)
ax.set_ylim(-0.5, 120)
ax.margins(y=0)

ax.legend(ncol = len(df.columns), loc="lower right", bbox_to_anchor=(0,1.02,1,0.08), 
          borderaxespad=0, title= 'Year' )


ax = plt.gca();
labels = df["plan_type"].tolist()
new_labels = []

for tick in labels:
word_list = tick.split();
new_labels.append(word_list);


new_ticks = []
for label_tick in new_labels:

if(len(label_tick) >= 2):

    joined = ' 
'.join(label_tick);
    new_ticks.append(joined) ;
else:
    joined = ' '.join(label_tick);
    new_ticks.append(joined) ;

plt.xticks(ticks = df.index, labels = new_ticks)
plt.xlabel('Type of Plan')
plt.ylabel('Count of Numbers')

这篇关于在海运条形图x轴Python中换行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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