Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between matplotlib's plot() and pandas plot()?

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?

like image 649
Abhay Avatar asked Aug 06 '26 12:08

Abhay


1 Answers

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

like image 79
filiphl Avatar answered Aug 08 '26 02:08

filiphl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!