Seaborn - 美学选择

时间:2015-08-08 06:48:20

标签: python pandas matplotlib plot seaborn

我正在使用seaborn factorplot绘制大熊猫数据。代码如下:

import seaborn
import numpy as np
import pandas as pd

seaborn.set_style("white")

fg = seaborn.factorplot(x='Group', y='val', hue='Mean', 
                        col='Sex', data=data, kind='bar', ci=68, row='Split', x_order=['Group 1', 'Group 2'],
                        row_order=['Poor','Good'], col_order=['Male', 'Female'], sharex=False, sharey=False, 
                        palette='deep', legend_out=False
                       )

(fg.set_axis_labels("", "val")
  .set_titles("{row_name} - {col_name}")
  .set(ylim=(0, 300))
  .despine(left=True)
) 

以下是一个示例数据框:

groups = ('Group 1', 'Group 2')
sexes = ('Male', 'Female')
means = ('Low', 'High')
split = ('Poor', 'Good')
index = pd.MultiIndex.from_product(
    [groups, sexes, means, split], 
   names=['Group', 'Sex', 'Mean', 'Split']
)

values = np.random.randint(low=20, high=100, size=len(index))
data = pd.DataFrame(data={'val': values}, index=index).reset_index()

我已经看过seaborn有哪些美学选择,还有一些我找不到参考:

  • 这会创建一个包含4个图的2x2网格。如何调整每个图之间的间距?现在,一切都太靠近了

  • 如何单独设置单独标签的字体大小?我希望某些轴/分类标签比其他标签更大

  • 如何完全删除图例标题?

  • 使用2x2网格图,是否可以将同一行中的2个图放在同一个图上?即连接x轴,删除右图上的y轴

  • 是否可以将绘图保存为基于矢量的文件格式?

1 个答案:

答案 0 :(得分:1)

fg = seaborn.factorplot(x='Group', y='val', hue='Mean', 
                        col='Sex', data=data, kind='bar',
                        ci=68, row='Split',
                        order=['Group 1', 'Group 2'],
                        row_order=['Poor','Good'],
                        col_order=['Male', 'Female'],
                        sharex=False, sharey="row", 
                        palette='deep', legend_out=False
                       )

(fg.set_axis_labels("", "val")
  .set_titles("{row_name} - {col_name}")
  .set(ylim=(0, 300))
  .despine(left=True)
)

# Increase space between rows and remove space between cols
fg.fig.subplots_adjust(hspace=.4, wspace=0)

# Change some label sizes
fg.axes[0, 0].title.set(size=16)
fg.axes[1, 0].yaxis.label.set(size=16)
plt.setp(fg.axes[0, 1].get_xticklabels(), size=16)

# Remove the legend title
fg.axes[0, 0].legend()

# Save to a vector format
fg.savefig("figure.svg")

enter image description here

请注意,除了额外的行之外,我在sharey="row"的调用中添加了factorplot,删除了y轴标记