【问题标题】:Plotting a Pandas series in Matplotlib/seaborn在 Matplotlib/seaborn 中绘制 Pandas 系列
【发布时间】:2019-08-22 16:07:34
【问题描述】:

我正在尝试另一种使用 matplotlib/seaborn 可视化熊猫系列的方法。但我做不到。有什么办法吗?

我可以使用 pandas 的 df.plot() 方法对其进行可视化。

df2.groupby('Company').Company.count()

数据如下所示:

100    a
101    b
102    c
103    d
104    a
105    c
106    d
107    b
108    a
109    c

【问题讨论】:

  • 您可以添加一些数据以及您要绘制的内容吗?通过 import seaborn as sns 然后使用 sns.set() 可以在使用 pandas plot 时获得 Seaborn 风格。
  • 我已经添加了示例数据。

标签: python python-3.x pandas matplotlib seaborn


【解决方案1】:

除了@Orysza 给出的答案之外,如果您希望系列排序 用于绘图,您可以使用系列的内置方法value_counts

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
tmp = pd.DataFrame()
tmp["vals"] = ["a", "b", "c", "d", "a", "c", "d", "b", "a", "c"]
tmp_valc = tmp["vals"].value_counts()
tmp_valc.head()

f, ax = plt.subplots(1, 1, figsize=(5,5))
g = sns.barplot(x=tmp_valc.index, y=tmp_valc)
t = g.set(title="Value counts of Pandas Series")

【讨论】:

    【解决方案2】:

    你可以使用 seaborn 的countplot:

    import matplotlib.pyplot as plt
    import seaborn as sns
    import pandas as pd
    test = pd.DataFrame()
    test["Company"] = ["a", "b", "c", "d", "a", "c", "d", "b", "a", "c"]
    ax=sns.countplot(test["Company"])
    plt.show()
    

    【讨论】:

    • 很好的建议。 +1
    猜你喜欢
    • 2020-04-24
    • 2018-02-13
    • 2018-03-29
    • 1970-01-01
    • 2020-06-20
    • 2017-12-09
    • 2020-08-31
    • 2014-10-06
    • 1970-01-01
    相关资源
    最近更新 更多