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

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

问题描述

我正在寻找如何在Seaborn中使用条形图显示数据框中而不是图形中的值的两件事

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)我正在寻找在数据框中绘制一个字段的值,同时绘制另一个字段的图形.例如,在下面,我正在绘制提示"图表,但我想将"total_bill"的值置于每个小节上方的中心位置(例如,星期五上方325.88, 星期六之上1778.40,等等.

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)有一种方法可以缩放条形的颜色,"total_bill"的最低值具有最浅的颜色(在本例中为星期五),而"total_bill"的最高值具有最暗的颜色.显然,缩放时我会坚持使用一种颜色(即蓝色).

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..

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

从以下代码开始,

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)

我得到以下结果:

临时解决方案:

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")

但这给了我以下错误:

AttributeError:"DataFrame"对象没有属性"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])

这让我留下了

IndexError:索引4超出了大小为4的轴0的范围

推荐答案

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

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()


第二次尝试也可以正常工作,唯一的问题是rank()返回的等级从1开始而不是零.因此必须从数组中减去1.同样对于索引,我们需要整数值,因此我们需要将其强制转换为int.


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天全站免登陆