vv4
vv4

Reputation: 75

Plotting a Pandas series in Matplotlib/seaborn

I am trying an alternate way to visualize a pandas series using 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.

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

Upvotes: 1

Views: 3430

Answers (2)

Srihari S
Srihari S

Reputation: 111

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

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

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

Graph of value counts

Upvotes: 1

Orysza
Orysza

Reputation: 714

You could use seaborn's 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()

showing the resulting graph

Upvotes: 3

Related Questions