【问题标题】:seaborn in Python : barplot appears upside downPython中的seaborn:条形图颠倒了
【发布时间】:2017-11-25 17:44:34
【问题描述】:

我正在学习 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')

条形图颠倒显示。

为什么会发生这种情况,我该如何纠正?

您的建议将不胜感激。

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

【问题讨论】:

  • 正如您不会调用sns.distplot(x=sns.barplot(x)) 左右一样,您也不应该将palplot 放入barplot 的参数中。

标签: python matplotlib bar-chart seaborn


【解决方案1】:

通过调用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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 2019-12-29
    • 2015-12-08
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 2019-09-05
    相关资源
    最近更新 更多