python - 如何使用 x = A 列,但颜色/色调 = B 列分类变量进行绘图

标签 python pandas bar-chart seaborn

我有一个 pandas 数据框,我想使用 Seaborn 创建一个条形图。问题是我想使用两个分类变量之一(例如 X 轴中的 A 列),但使用不同的分类列(例如 B 列)来为条形着色。 B 中的值可以代表 A 中的多个值。

        MajorCategories               name                 review_count    
Food,Restaurants              Mon Ami Gabi                  8348
Food,Restaurants              Bacchanal Buffet              8339
Restaurants                   Wicked Spoon                  6708
Food,Restaurants              Hash House A Go Go            5763
Restaurants                   Gordon Ramsay BurGR           5484
Restaurants                   Secret Pizza                  4286
Restaurants                   The Buffet at Bellagio        4227
Hotels & Travel              McCarran International Airport 3627
Restaurants                  Yardbird Southern Table & Bar  3576

所以,我希望我的条形图能够用 x = 'name' 和 y='review_count' 绘制条形图,同时绘制颜色/色调? = 主要类别。在 Seaborn 中可以不用很多行代码吗?

以下是我在seaborn 中获取的图像以及我想要获取的图像的链接。

sns.catplot(x="review_count", y="name", kind="bar", data=plot_data,  aspect= 1.5)

我使用上面的代码使用seaborn得到的图

Plot I get using seaborn using the code above

我想要实现的图,这个是在 R 中使用 ggplot2 Plot I am trying to achieve, this one is using ggplot2 in R

最佳答案

尝试传递hue并设置dodge=False:

sns.catplot(x="review_count", y="name", hue='MajorCategories',
            kind="bar", data=plot_data,
            dodge=False, aspect= 1.5)

输出:

enter image description here

关于python - 如何使用 x = A 列,但颜色/色调 = B 列分类变量进行绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56310736/

相关文章:

多处理期间的 Python stdout

python - 插入时间序列并重新采样/旋转。如何得到预期的输出

r - ggplot : weighted. 多面条形图中的平均值和 stat_summary

python - Django 表单中的可编辑选择字段/下拉框

python - javascript 的 _.where 在 python 中

python - 以同样的方式转换 CSV 文件的文件夹,然后使用 python 输出多个数据帧

python - 使用 spacy 从数据框中提取实体

python - 根据公共(public)字段将两行合并为一行

javascript - 在 chart.js 的堆叠条形图中显示百分比和总数

r - 堆积条形图的位置图例