简体   繁体   English

在Matplotlib / seaborn中绘制熊猫系列

[英]Plotting a Pandas series in Matplotlib/seaborn

I am trying an alternate way to visualize a pandas series using matplotlib/seaborn. 我正在尝试使用matplotlib / seaborn可视化熊猫系列的另一种方法。 But I am not able to do it. 但是我做不到。 Is there any way? 有什么办法吗?

I have no problem visualizing it using the df.plot() method of pandas. 我使用熊猫的df.plot()方法可视化它没有问题。

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

Data looks like this: 数据如下所示:

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

You could use seaborn's countplot : 您可以使用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()

显示结果图

Adding on to the answer given by @Orysza , in case you want the Series sorted for plotting, you could use the Series' in-built method value_counts 加上@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()

在value_counts()之后输出

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

价值计数图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM