Reputation: 23
I have dataset for authors and respective book title. Sample data as follows:
Author Title
A abc
B xyz
C pqr
E asd
R wer
I want to plot a graph which should show the frequency of book written by each author.To do that first I am using value_counts() function.
books_df.Author.value_counts()
and I am getting this output:
A 6
C 4
R 3
D 3
E 3
S 1
B 1
Name: Author, dtype: int64
I can easily get the bar chart by using following code:
plt.bar(books_df.Author.unique(),books_df.Author.value_counts())
But when I am trying to generate pie chart using this :
plt.pie(books_df.value_counts())
I am getting error 'DataFrame' object has no attribute 'value_counts'
Where I am doing wring?
Expected output:
A pie chart with book ratio and author names AND author with highest number of books should be stand out.
Upvotes: 2
Views: 64
Reputation: 663
You are doing a silly mistake here. Instead of using plt.pie(books_df.value_counts())
you should use plt.pie(books_df.Author.value_counts())
. You forgot to add 'Author' here.
Now as per your output requirements, You can follow these steps:
1) Create a label with a unique list of authors:
label = books_df.Author.value_counts().index
2) Create size:
sizes = (books_df.Author.value_counts()).tolist()
3) To highlight the Author slice with the highest number of books:
explode = (0.1, 0, 0, 0,0,0,0)
4) To plot the graph:
plt.pie(sizes, explode=explode, labels=labels,
autopct='%1.1f%%',shadow=True, startangle=140)
plt.axis('equal')
plt.show()
5) You will get something like this:
Upvotes: 2