python - Python 中的seaborn : barplot appears upside down

标签 python matplotlib bar-chart seaborn

我正在学习seaborn,并且遇到了一些意想不到的行为。

这个可重现的示例使用了 Surveys.csv 数据集,可以在此链接中找到:http://www.datacarpentry.org/python-ecology-lesson/setup/

我的代码如下:

import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="whitegrid", color_codes=True)

surveys_df = pd.read_csv("surveys.csv")
avg_weight = surveys_df.groupby("plot_id")["weight"].mean().to_frame()
avg_weight
    weight
plot_id 
1   51.822911
2   52.251688
3   32.654386
4   47.928189
5   40.947802
6   36.738893
7   20.663009
8   47.758001
9   51.432358
10  18.541219
11  43.451757
12  49.496169
13  40.445660
14  46.277199
15  27.042578
16  24.585417
17  47.889593
18  40.005922
19  21.105166
20  48.665303
21  24.627794
22  54.146379
23  19.634146
24  43.679167

sns.barplot(x = avg_weight.index.values, y = "weight", 
            data = avg_weight, palette = sns.palplot(sns.diverging_palette(150, 275, s=80, l=55, n=9)))
plt.xlabel('Animal id')
plt.ylabel('Average Weight')
plt.title('Average Weight by Animal')

enter image description here

条形图出现颠倒。

为什么会发生这种情况以及如何纠正它?

我们将不胜感激您的建议。

PS:不知何故,这个问题与传递给调色板参数的值有关,因为当我选择 palette = sns.color_palette("coolwarm", 7) 时它就得到了解决。尽管如此,我还是不明白为什么。

最佳答案

通过调用sns.palplot ,您正在制作另一个图,这会导致图窗属性设置不正确。删除它,你应该就好了:

sns.barplot(x = avg_weight.index.values, y = "weight", 
            data = avg_weight, palette = sns.diverging_palette(150, 275, s=80, l=55, n=9))
plt.xlabel('Animal id')
plt.ylabel('Average Weight')
plt.title('Average Weight by Animal')

enter image description here

关于python - Python 中的seaborn : barplot appears upside down,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47488856/

相关文章:

python - 如何在 Web 服务器(例如本地主机)上运行 Python 脚本

python - 使用 Django-Filters 有条件地渲染字段

python - 将 imshow 与用户输入相结合

python - 在 matplotlib 图中绘制 SVG(嵌入在 wxPython 中)

javascript - 带有重要性星星的条形图

python - Windows 机器上使用 Python 2.7 和 3.4 的 Pip

python - 如何避免 python Tkinter 中的键盘冲突?

python - 由于 GNU Radio block 的快速输入流而导致绘图卡住

javascript - 使用 morris.js 显示 "Bar"表示零结果

python - 在 MATPLOTLIB 中一一显示两个条形图