如何使用 seaborn 处理长轴标签?
How to handle long axis labels with seaborn?
我有一些用 seaborn
制作的图表。我按某些小组绘制箱线图和其他东西。我举个例子来说明一下:
如您所见,x 标签太长了。我将它们旋转 90 度只是为了让它们易于阅读。我可以去手动更改类别名称。但是,我需要每周执行此任务 - 并且类别会随着时间而改变。您知道如何解决这个问题吗?
为了更好的衡量,我会告诉你我到目前为止所做的一切。给定一个虚拟数据集,这基本上就是我对 pandas 数据框 df
:
所做的
rank sentiment category
0 1 0.657413 super_long_string
1 2 0.895769 super_long_string
2 3 -0.435457 super_long_string
3 4 -0.717959 other_super_long_string
4 5 0.869688 other_super_long_string
ax =sns.boxplot(x='category', y=sentiment, data=df);
ax.set_xticklabels(ax.get_xticklabels(),rotation=90)
plt.figure()
有没有办法在不丢失信息的情况下去除长 x 轴标签的方式?我的想法是将标签保留在图例中。也许类似于这个例子? seaborn 可以实现吗?如果可以,怎么做?
有什么想法吗?非常感谢您!
当然,您可以尝试通过翻转 x
和 y
水平制作箱线图
ax =sns.boxplot(y='category', x=sentiment, data=df);
您还可以生成一个自定义图例,在其中设置一个带有每个箱线图颜色的色块
import pandas as pd
import seaborn.apionly as sns
import scipy as sp
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
labels = ['this is some really really long label text, so long that it is much better read horizontally',
'and another really long label, this could probably be very much reduced',
'a short label for a change',
'lorem ipsum',
'and another really long and very descriptive label, including some abbreviations a.n.g']
Df = pd.DataFrame(sp.randn(len(labels),10))
Df['labels'] = labels
fig, ax = plt.subplots()
ax = sns.boxplot(data=Df.melt(id_vars='labels'),x='labels',y='value',ax=ax)
ax.set_xticklabels('')
leg_handles = []
for label,artist in zip(labels,ax.artists):
handle = mpatches.Patch(facecolor=artist.get_facecolor(),label=label)
leg_handles.append(handle)
ax.legend(handles=leg_handles,bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=1, mode="expand", borderaxespad=0.)
fig.subplots_adjust(top=0.75)
我有一些用 seaborn
制作的图表。我按某些小组绘制箱线图和其他东西。我举个例子来说明一下:
如您所见,x 标签太长了。我将它们旋转 90 度只是为了让它们易于阅读。我可以去手动更改类别名称。但是,我需要每周执行此任务 - 并且类别会随着时间而改变。您知道如何解决这个问题吗?
为了更好的衡量,我会告诉你我到目前为止所做的一切。给定一个虚拟数据集,这基本上就是我对 pandas 数据框 df
:
rank sentiment category
0 1 0.657413 super_long_string
1 2 0.895769 super_long_string
2 3 -0.435457 super_long_string
3 4 -0.717959 other_super_long_string
4 5 0.869688 other_super_long_string
ax =sns.boxplot(x='category', y=sentiment, data=df);
ax.set_xticklabels(ax.get_xticklabels(),rotation=90)
plt.figure()
有没有办法在不丢失信息的情况下去除长 x 轴标签的方式?我的想法是将标签保留在图例中。也许类似于这个例子? seaborn 可以实现吗?如果可以,怎么做?
有什么想法吗?非常感谢您!
当然,您可以尝试通过翻转 x
和 y
ax =sns.boxplot(y='category', x=sentiment, data=df);
您还可以生成一个自定义图例,在其中设置一个带有每个箱线图颜色的色块
import pandas as pd
import seaborn.apionly as sns
import scipy as sp
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
labels = ['this is some really really long label text, so long that it is much better read horizontally',
'and another really long label, this could probably be very much reduced',
'a short label for a change',
'lorem ipsum',
'and another really long and very descriptive label, including some abbreviations a.n.g']
Df = pd.DataFrame(sp.randn(len(labels),10))
Df['labels'] = labels
fig, ax = plt.subplots()
ax = sns.boxplot(data=Df.melt(id_vars='labels'),x='labels',y='value',ax=ax)
ax.set_xticklabels('')
leg_handles = []
for label,artist in zip(labels,ax.artists):
handle = mpatches.Patch(facecolor=artist.get_facecolor(),label=label)
leg_handles.append(handle)
ax.legend(handles=leg_handles,bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=1, mode="expand", borderaxespad=0.)
fig.subplots_adjust(top=0.75)