python 三个变量-其他可视化图

1、两个分类变量和一个数值变量

ax = sb.barplot(data = df, x = 'cat_var1', y = 'num_var2', hue = 'cat_var2')
ax.legend(loc = 8, ncol = 3, framealpha = 1, title = 'cat_var2')

在这里插入图片描述
2、“hue” 参数也可以在函数 boxplot, violinplot 和 pointplot 中完成一样的功能,以分组显示的方式添加第三个分类变量。

pointplot 的温馨提示:默认会将分类变量的所有类别以垂直对齐的方式呈现。使用 “dodge” 参数可以将这些类别不再重叠,转换成分组显示的形式:

ax = sb.pointplot(data = df, x = 'cat_var1', y = 'num_var2', hue = 'cat_var2',dodge = 0.3, linestyles = "")

在这里插入图片描述

3、折线图 中添加分类变量,将其调整为多折线图

def mean_poly(x, y, bins = 10, **kwargs):
    """ Custom adapted line plot code. """
    # set bin edges if none or int specified
    if type(bins) == int:
        bins = np.linspace(x.min(), x.max(), bins+1)
    bin_centers = (bin_edges[1:] + bin_edges[:-1]) / 2

    # compute counts
    data_bins = pd.cut(x, bins, right = False,
                       include_lowest = True)
    means = y.groupby(data_bins).mean()

    # create plot
    plt.errorbar(x = bin_centers, y = means, **kwargs)

bin_edges = np.arange(0.25, df['num_var1'].max()+0.5, 0.5)
g = sb.FacetGrid(data = df, hue = 'cat_var2', size = 5)
g.map(mean_poly, "num_var1", "num_var2", bins = bin_edges)
g.set_ylabels('mean(num_var2)')
g.add_legend()

在这里插入图片描述

发布了185 篇原创文章 · 获赞 6 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/JackLi31742/article/details/105012791