7

What is the difference between matplotlib.pyplot.plot() and pandas.DataFrame.plot()?

We can plot using both but what is the measure difference between both?

How can i draw bar chart and group by some categorical variable?

1 Answer 1

6

Matplotlib's pyplot is the library that Pandas use in their plot function. Pandas' plot is only a convenient shortcut. For the bar chart question: I would suggest using Seaborn's barplot, using the desired category as hue. If you wish to only use Pandas, then maybe something like:

df = pd.DataFrame(np.random.rand(10, 1), columns=['col_name'])
df['category'] = df.col_name>0.5

print(df)

   col_name  category
0  0.053908     False
1  0.136295     False
2  0.325790     False
3  0.362942     False
4  0.919924      True
5  0.406884     False
6  0.433959     False
7  0.725699      True
8  0.582537      True
9  0.608040      True

df.groupby('category').count().plot(kind='bar', legend=False)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.