Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I have the following code:

import matplotlib.pyplot as plt

cdict = {

  'red'  :  ( (0.0, 0.25, .25), (0.02, .59, .59), (1., 1., 1.)),

  'green':  ( (0.0, 0.0, 0.0), (0.02, .45, .45), (1., .97, .97)),

  'blue' :  ( (0.0, 1.0, 1.0), (0.02, .75, .75), (1., 0.45, 0.45))

}

cm = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024)

plt.clf()

plt.pcolor(X, Y, v, cmap=cm)

plt.loglog()

plt.xlabel('X Axis')

plt.ylabel('Y Axis')

plt.colorbar()

plt.show()

So this produces a graph of the values 'v' on the axes X vs Y, using the specified colormap. The X and Y axes are perfect, but the colormap spreads between the min and max of v. I would like to force the colormap to range between 0 and 1.

1 Answer

0 votes
by (25.1k points)
edited by

Since you are trying yo plot a pie chart you can just store the means of columns in a column total titles Total and then plot that. You can do it like this:

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

If you want to get a deeper understanding of data science and libraries used in it you can watch this youtube video: 

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...