【发布时间】:2021-08-27 12:09:45
【问题描述】:
我正在可视化调查结果。答案很长,我想将它们完全放入图表中。因此,如果您能指出一种使用多行 xticklabels 的方法,或者将 xticklabels 包含在侧面的图例中,我将非常感激,如本示例所示:
因为否则我必须使图表非常宽以适应整个答案。我当前的代码和结果图如下所示:
import seaborn as sns
from textwrap import wrap
sns.set(style="dark")
catp = (sns.catplot(data=results, x='1',
kind='count',
hue_order=results.sort_values('1')['1'],
palette='crest',
height=3.3,
aspect=17.4/7)
.set(xlabel=None,
ylabel='Number of Participants',
title="\n".join(wrap("Question 1: Out of the three options, please choose the one you would prefer your fully autonomous car to choose, if you sat in it.", 90)))
)
plt.tight_layout()
catp.ax.set_yticks((0,10,20,30,40))
for p in catp.ax.patches:
percentage = '{:.1f}%'.format(100 * p.get_height()/92)
x = p.get_x() + p.get_width() / 2 - 0.05
y = p.get_y() + p.get_height() + 0.3
catp.ax.annotate(percentage, (x, y), size = 12)
plt.show()
最好的问候!
编辑:您可以使用以下代码创建示例数据框:
import pandas as pd
import numpy as np
from itertools import chain
x = (np.repeat('Brake and crash into the bus', 37),
np.repeat('Steer into the passing car on the left', 22),
np.repeat('Steer into the right hand sidewall', 39))
results = pd.DataFrame({'1': list(chain(*x))})
【问题讨论】:
标签: python matplotlib plot graph seaborn