Python Seaborn Barplot - 显示值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43214978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Seaborn Barplot - Displaying Values
提问by Stumbling Through Data Science
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
我正在寻找如何在 Seaborn 中使用条形图来显示数据框中但不在图形中的值来做两件事
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.)
1)我希望在绘制另一个字段的同时显示数据框中一个字段的值。例如,在下面,我正在绘制“小费”图形,但我想将“total_bill”的值放在每个条形上方的中心位置(即周五上方 325.88,周六上方 1778.40 等)
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.
2)有没有办法缩放条形的颜色,'total_bill'的最低值具有最浅的颜色(在这种情况下为星期五),'total_bill'的最高值具有最暗的颜色。显然,当我进行缩放时,我会坚持使用一种颜色(即蓝色)。
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.
虽然我看到其他人认为这是另一个(或两个)问题的重复,但我遗漏了我如何使用不在图中的值作为标签或阴影的基础的部分。怎么说呢,以total_bill为基础。我很抱歉,但我无法根据这些答案弄清楚。
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:
在shading,使用下面的示例,我尝试了以下操作:
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'
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])
and that leaves me with
这给我留下了
IndexError: index 4 is out of bounds for axis 0 with size 4
索引错误:索引 4 超出轴 0 的范围,大小为 4
回答by ImportanceOfBeingErnest
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.
让我们坚持链接问题的解决方案(更改 seaborn 条形图中的颜色比例)。您想使用 argsort 来确定用于为条形着色的颜色顺序。在链接的问题中 argsort 应用于 Series 对象,它工作正常,而在这里你有一个 DataFrame。因此,您需要选择该 DataFrame 的一列来应用 argsort。
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()
rank()
在开始1
1
,而不是零。所以必须从数组中减去 1。同样对于索引,我们需要整数值,因此我们需要将其强制转换为int
int
.rank = groupedvalues['total_bill'].rank(ascending=True).values
rank = (rank-1).astype(np.int)
回答by Sharon Soussan
Works with single ax or with matrix of ax (subplots)
适用于单轴或轴矩阵(子图)
from matplotlib import pyplot as plt
import numpy as np
def show_values_on_bars(axs):
def _show_on_single_plot(ax):
for p in ax.patches:
_x = p.get_x() + p.get_width() / 2
_y = p.get_y() + p.get_height()
value = '{:.2f}'.format(p.get_height())
ax.text(_x, _y, value, ha="center")
if isinstance(axs, np.ndarray):
for idx, ax in np.ndenumerate(axs):
_show_on_single_plot(ax)
else:
_show_on_single_plot(axs)
fig, ax = plt.subplots(1, 2)
show_values_on_bars(ax)
回答by Secant Zhang
Just in case if anyone is interested in labeling horizontal barplot graph, I modified Sharon's answeras below:
以防万一如果有人对标记水平条形图感兴趣,我修改了Sharon 的答案如下:
def show_values_on_bars(axs, h_v="v", space=0.4):
def _show_on_single_plot(ax):
if h_v == "v":
for p in ax.patches:
_x = p.get_x() + p.get_width() / 2
_y = p.get_y() + p.get_height()
value = int(p.get_height())
ax.text(_x, _y, value, ha="center")
elif h_v == "h":
for p in ax.patches:
_x = p.get_x() + p.get_width() + float(space)
_y = p.get_y() + p.get_height()
value = int(p.get_width())
ax.text(_x, _y, value, ha="left")
if isinstance(axs, np.ndarray):
for idx, ax in np.ndenumerate(axs):
_show_on_single_plot(ax)
else:
_show_on_single_plot(axs)
Two parameters explained:
两个参数解释:
h_v
- Whether the barplot is horizontal or vertical. "h"
represents the horizontal barplot, "v"
represents the vertical barplot.
h_v
- 条形图是水平的还是垂直的。"h"
代表水平条形图,"v"
代表垂直条形图。
space
- The space between value text and the top edge of the bar. Only works for horizontal mode.
space
- 值文本和条形顶部边缘之间的空间。仅适用于水平模式。
Example:
例子:
show_values_on_bars(sns_t, "h", 0.3)
回答by ańō??Xmo??
Hope this helps for item #2: a) You can sort by total bill then reset the index to this column b) Use palette="Blue" to use this color to scale your chart from light blue to dark blue (if dark blue to light blue then use palette="Blues_d")
希望这对第 2 项有所帮助:a)您可以按总账单排序,然后将索引重置为此列 b)使用 Palette="Blue" 使用此颜色将图表从浅蓝色缩放到深蓝色(如果深蓝色到浅蓝色然后使用调色板=“Blues_d”)
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()
groupedvalues=groupedvalues.sort_values('total_bill').reset_index()
g=sns.barplot(x='day',y='tip',data=groupedvalues, palette="Blues")
回答by user3663280
plt.figure(figsize=(15,10))
graph = sns.barplot(x='name_column_x_axis', y="name_column_x_axis", data = dataframe_name , color="salmon")
for p in graph.patches:
graph.annotate('{:.0f}'.format(p.get_height()), (p.get_x()+0.3, p.get_height()),
ha='center', va='bottom',
color= 'black')