Pyplot error only size-1 arrays can be converted to Python scalars

0 votes

I am trying to plot pie chart using the below code, but I am getting the error "only size-1 arrays can be converted to Python scalars". Can you help me resolve the issue.

Complete code:

import pandas as pd
import matplotlib.pyplot as plt
dir(pd)
prisoner_df=pd.read_csv("prisoners.csv")

#Syntax to display last and first 5 rows of records
print(prisoner_df.tail())
print(prisoner_df.head())

dir(prisoner_df)

# Syntax to get only non zero values of rows

new_value=prisoner_df.iloc[:,[2,3,4,5]].mean(axis=1) >0
prisoner_df[new_value]

# create new columns
prisoner_df["total_benefitted"]=prisoner_df.sum(axis=1)
series_value=pd.Series(prisoner_df.sum(axis=0))
series_value

prisoner_df.append(series_value,ignore_index=True)
prisoner_df.at[35, "STATE/UT"]="Totals"

#Plotting

#plot with each state name on the x -axis and their total benefitted
plt.pie(prisoner_df.iloc[35:,[2,3,4,5,6]])
Jan 28, 2019 in Python by Nisha
9,485 views

1 answer to this question.

+1 vote

You can plot the chart by taking the total column to plot the group. Refer to the code below:

import pandas as pd
import matplotlib.pyplot as plt

prisoner_df=pd.read_csv("prisoners.csv")

# Syntax to get only non zero values of rows

prisoner_df['Total']= prisoner_df.iloc[:,[2,3,4,5]].mean(axis=1)

#print(prisoner_df.head())

#Plotting
labels=[]
states=prisoner_df['STATE/UT']
total=prisoner_df['Total']
patches, texts = plt.pie(total,shadow=True,startangle=90)
#plt.legend(patches, states)
plt.axis('equal')
plt.tight_layout()
plt.show()

Hope this helps!

If you need to know more about Scala, join Scala course today and become the expert.

Thanks!!

answered Jan 28, 2019 by Omkar
• 69,220 points

Related Questions In Python

0 votes
0 answers

Pyplot error "only size-1 arrays can be converted to Python scalars"

I am getting the following error while ...READ MORE

Nov 28, 2019 in Python by Sumit
• 130 points
3,743 views
0 votes
0 answers

Python error "only size-1 arrays can be converted to Python scalars"

I'm trying to plot the exponential and ...READ MORE

Jul 9, 2019 in Python by JTM
1,805 views
0 votes
0 answers

TypeError: only size-1 arrays can be converted to Python scalars

This is my code for key in bboxes: bbox ...READ MORE

May 19, 2020 in Python by Rumia
• 120 points

edited May 19, 2020 by Gitika 1,609 views
+1 vote
0 answers

TypeError: only integer scalar arrays can be converted to a scalar index

import cv2 import numpy as np import matplotlib.pyplot as ...READ MORE

Dec 18, 2019 in Python by sandeep
• 130 points
7,381 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,449 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,100 views
+3 votes
2 answers

how to print array integer without [] bracket in python like result = 1,2,3,4,5

Hey @abhijmr.143, you can print array integers ...READ MORE

answered Aug 5, 2018 in Python by Omkar
• 69,220 points

edited Aug 8, 2018 by Omkar 8,167 views
0 votes
1 answer

How to list only text files in a directory using python?

Well, you are using a complex way. ...READ MORE

answered Feb 4, 2019 in Python by Omkar
• 69,220 points
2,756 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP