Seaborn Barplot - 显示值 [英] Seaborn Barplot - Displaying Values

查看:119
本文介绍了Seaborn Barplot - 显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看如何在 Seaborn 中通过使用条形图来显示数据框中而不是图形中的值来做两件事

1) 我希望在绘制另一个字段的同时显示数据框中一个字段的值.例如,在下面,我正在绘制小费"的图形,但我想将total_bill"的值放在每个条形上方的中心位置(即星期五上方的 325.88,周六 1778.40 以上等)

2) 有没有办法缩放条形的颜色,'total_bill' 的最低值具有最亮的颜色(在本例中为星期五),'total_bill' 的最高值具有最暗的颜色.显然,当我进行缩放时,我会坚持使用一种颜色(即蓝色).

谢谢!我确定这很容易,但我很想念它..

虽然我看到其他人认为这是另一个(或两个)问题的重复,但我遗漏了我如何使用不在图中的值作为标签或阴影.怎么说呢,以total_bill为基础.抱歉,我无法根据这些答案弄清楚.

从以下代码开始,

将pandas导入为pd将 seaborn 作为 sns 导入%matplotlib 内联df=pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-book/master/ch08/tips.csv", sep=',')groupedvalues=df.groupby('day').sum().reset_index()g=sns.barplot(x='day',y='tip',data=groupedvalues)

我得到以下结果:

临时解决方案:

 用于索引,groupedvalues.iterrows() 中的行:g.text(row.name,row.tip, round(row.total_bill,2), color='black', ha="center")

shading 上,使用下面的示例,我尝试了以下操作:

将pandas导入为pd将 seaborn 作为 sns 导入%matplotlib 内联df=pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-book/master/ch08/tips.csv", sep=',')groupedvalues=df.groupby('day').sum().reset_index()pal = sns.color_palette("Greens_d", len(data))rank = groupedvalues.argsort().argsort()g=sns.barplot(x='day',y='tip',data=groupedvalues)对于索引,groupedvalues.iterrows() 中的行:g.text(row.name,row.tip, round(row.total_bill,2), color='black', ha="center")

但这给了我以下错误:

AttributeError: 'DataFrame' 对象没有属性 'argsort'

所以我尝试了一个修改:

将pandas导入为pd将 seaborn 作为 sns 导入%matplotlib 内联df=pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-book/master/ch08/tips.csv", sep=',')groupedvalues=df.groupby('day').sum().reset_index()pal = sns.color_palette("Greens_d", len(data))rank=groupedvalues['total_bill'].rank(升序=真)g=sns.barplot(x='day',y='tip',data=groupedvalues,palette=np.array(pal[::-1])[rank])

这就给我留下了

索引错误:索引 4 超出轴 0 的范围,大小为 4

解决方案

让我们坚持链接问题中的解决方案 (

<小时>第二次尝试也很好,唯一的问题是 rank() 返回的排名从 1 而不是零开始.所以必须从数组中减去 1.同样对于索引,我们需要整数值,因此我们需要将其转换为 int.

rank = groupedvalues['total_bill'].rank(ascending=True).values等级 = (rank-1).astype(np.int)

I'm looking to see how to do two things in Seaborn with using a bar chart to display values that are in the dataframe, but not in the graph

1) I'm looking to display the values of one field in a dataframe while graphing another. For example, below, I'm graphing 'tip', but I would like to place the value of 'total_bill' centered above each of the bars (i.e.325.88 above Friday, 1778.40 above Saturday, etc.)

2) Is there a way to scale the colors of the bars, with the lowest value of 'total_bill' having the lightest color (in this case Friday) and the highest value of 'total_bill' having the darkest. Obviously, I'd stick with one color (i.e. blue) when I do the scaling.

Thanks! I'm sure this is easy, but i'm missing it..

While I see that others think that this is a duplicate of another problem (or two), I am missing the part of how I use a value that is not in the graph as the basis for the label or the shading. How do I say, use total_bill as the basis. I'm sorry, but I just can't figure it out based on those answers.

Starting with the following code,

import pandas as pd
import seaborn as sns
%matplotlib inline
df=pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-    book/master/ch08/tips.csv", sep=',')
groupedvalues=df.groupby('day').sum().reset_index()
g=sns.barplot(x='day',y='tip',data=groupedvalues)

I get the following result:

Interim Solution:

for index, row in groupedvalues.iterrows():
    g.text(row.name,row.tip, round(row.total_bill,2), color='black', ha="center")

On the shading, using the example below, I tried the following:

import pandas as pd
import seaborn as sns
%matplotlib inline
df=pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-book/master/ch08/tips.csv", sep=',')
groupedvalues=df.groupby('day').sum().reset_index()

pal = sns.color_palette("Greens_d", len(data))
rank = groupedvalues.argsort().argsort() 
g=sns.barplot(x='day',y='tip',data=groupedvalues)

for index, row in groupedvalues.iterrows():
    g.text(row.name,row.tip, round(row.total_bill,2), color='black', ha="center")

But that gave me the following error:

AttributeError: 'DataFrame' object has no attribute 'argsort'

So I tried a modification:

import pandas as pd
import seaborn as sns
%matplotlib inline
df=pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-book/master/ch08/tips.csv", sep=',')
groupedvalues=df.groupby('day').sum().reset_index()

pal = sns.color_palette("Greens_d", len(data))
rank=groupedvalues['total_bill'].rank(ascending=True)
g=sns.barplot(x='day',y='tip',data=groupedvalues,palette=np.array(pal[::-1])[rank])

and that leaves me with

IndexError: index 4 is out of bounds for axis 0 with size 4

解决方案

Let's stick to the solution from the linked question (Changing color scale in seaborn bar plot). You want to use argsort to determine the order of the colors to use for colorizing the bars. In the linked question argsort is applied to a Series object, which works fine, while here you have a DataFrame. So you need to select one column of that DataFrame to apply argsort on.

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

df = sns.load_dataset("tips")
groupedvalues=df.groupby('day').sum().reset_index()

pal = sns.color_palette("Greens_d", len(groupedvalues))
rank = groupedvalues["total_bill"].argsort().argsort() 
g=sns.barplot(x='day',y='tip',data=groupedvalues, palette=np.array(pal[::-1])[rank])

for index, row in groupedvalues.iterrows():
    g.text(row.name,row.tip, round(row.total_bill,2), color='black', ha="center")

plt.show()


The second attempt works fine as well, the only issue is that the rank as returned by rank() starts at 1 instead of zero. So one has to subtract 1 from the array. Also for indexing we need integer values, so we need to cast it to int.

rank = groupedvalues['total_bill'].rank(ascending=True).values
rank = (rank-1).astype(np.int)

这篇关于Seaborn Barplot - 显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆