seaborn violinplots:改变小提琴颜色,轴名称,图例

时间:2015-10-20 19:04:17

标签: python matplotlib seaborn

我曾经使用matplotlib制作我的所有情节,直到我最近发现seaborn。我发现特别有趣的是,它允许你拆分小提琴以比较给定的hue变量。

好吧,我的主要问题是我不知道我需要修改什么才能更改小提琴颜色轴名称位置传奇我想要的地方。

以下是我从seaborn

开始的示例
    import seaborn as sns
    tips = sns.load_dataset("tips")
    sns.set(style="ticks", palette="colorblind")
    g = sns.FacetGrid(tips, col="time", size=4, aspect=.75)
    g = g.map(sns.violinplot, "sex", "total_bill", "smoker", inner=None, linewidth=1, scale="area", split=True, width=0.75).despine(left=True).add_legend(title="smoker")
    g.savefig(os.path.join(options.output_dir, "figures", "violinplots.png"))

这是输出数字violinplots.png

enter image description here

虽然我更喜欢这样的事情:

enter image description here

总结:

  • 使用whiteblue颜色
  • 替换轴names
  • 仅限leftmost y axis
  • 制作我自己的legend with the blue category only

提前谢谢你。欢迎任何帮助。

如果有人对此感兴趣,感谢MrPedru22

,这就是我最终解决这个问题的方法
import seaborn as sns
tips = sns.load_dataset("tips")
sns.set(context="paper", palette="colorblind", style="ticks")
g = sns.FacetGrid(tips, col="time", sharey=False, size=4, aspect=.5)
g = g.map(seaborn.violinplot, "sex", "total_bill", "smoker", cut=0, inner=None, split=True, palette={"No": "#4477AA", "Yes": "white"}, saturation=1).despine(left=True)
# Set axis labels & ticks #
g.fig.get_axes()[0].set_xlabel("Lunch")
g.fig.get_axes()[1].set_xlabel("Dinner")
g.fig.get_axes()[0].set_xticklabels(["Male", "Female"])
g.fig.get_axes()[1].set_xticklabels(["Male", "Female"])
g.fig.get_axes()[0].set_ylabel("Total bill")
g.fig.get_axes()[0].set_yticks(range(0, 80, 10))
g.fig.get_axes()[1].set_yticks([])
g.fig.get_axes()[0].spines["left"].set_visible(True)
# Set legend #
handles, labels = g.fig.get_axes()[0].get_legend_handles_labels()
g.fig.get_axes()[0].legend([handles[1]], ["Non-smoker"], loc='upper left')
# Fixing titles #
g.fig.get_axes()[0].set_title("")
g.fig.get_axes()[1].set_title("")
g.plt.show()

enter image description here

1 个答案:

答案 0 :(得分:5)

以下是一些答案:

import seaborn as sns
tips = sns.load_dataset("tips")
sns.set(style="ticks", palette="colorblind")
g = sns.FacetGrid(tips, col="time", size=4, aspect=.75)
g = g.map(sns.violinplot, "sex", "total_bill", "smoker", palette={"No": "b", "Yes": "w"}, inner=None, linewidth=1, scale="area", split=True, width=0.75).despine(left=True)
g.fig.get_axes()[0].legend(title= 'smoker',loc='upper left')
g.set_axis_labels('lunch','total bill')
sns.plt.show()