Pandas DataFrame Plot pie graph

Pandas plot

Pandas.DataFrame.plot to generate pie using data
DataFrame pie chart



Python Pandas Plot Pie chart by using DataFrame with options & save as image
import pandas as pd 
my_dict={
'NAME':['Ravi','Raju','Alex','Ronn'],
'MATH':[30,40,50,50]
}
df = pd.DataFrame(data=my_dict)
df.plot.pie(title="Std Mark",y='MATH')

pie chart with options

There are several options we can add to above pie diagram.

figsize :

figsize=(4,4)
Size of the graph , it is a tuple saying width and height in inches, figsize=(6,3). Here width is 6 inches and height is 3 inches. Since our pia chart is a circle so better to use equal width and height.
df.plot.pie(title="Std Mark",y='MATH',figsize=(4,4))

fontsize

fontsize=20 , we can set the font size used labels in x and y axis. fontsize=20
df.plot.pie(title="Std Mark",
y='MATH',fontsize=20)

labels

lables option for pia diagram We can define our labels by using list.
my_labels=['Ravi','Raju','Alex','Ronn']
df.plot.pie(title="Std Mark",y='MATH',
            fontsize=20,labels=my_labels)

labeldistance

Distance of Label from center of the Pie chart, default is 1.1 (float ).

legend

Default value is True, we can remove by using legend=False

explode

We can add option to explode one segment by using a tuple. explode option for pia diagram
my_explode=(0,0,0.1,0)
df.plot.pie(title="Std Mark",y='MATH',
        fontsize=20,explode=my_explode)

We can change the value ( from 0 ) to move the segments more distance. Here we are moving the first segment more. explode option for pia diagram
my_explode=(1.5,0.0,0.1,0)
df.plot.pie(title="Std Mark",
 y='MATH',explode=my_explode)

startangle

startangle option for pia diagram
We can add option startangle=40 to give the initial starting point for the first segment. 0 degree is the base from which anti clockwise the angle is checked.
df.plot.pie(title="Std Mark",y='MATH',
        fontsize=20,startangle=40)

counterclock

Default value is True. The direction of fractions.

rotatelabels

Default is False, Rotate each label matching to slice in degree.

shadow=True


shadow=True option for pie diagram shadow=False option for pie diagram








df.plot.pie(title="shadow=False",y='MATH',
        figsize=(5,5),shadow=False)

colors

colors option for pie diagram We can use the option colors to give different colors to segments. We can use one tuple to define the colours.
my_colors=['lightblue','lightgreen',
           'silver','green']
df.plot.pie(title="Colors",y='MATH',
        figsize=(5,5),colors=my_colors)

autopct

autopct option for pie diagram To display the percentage of each segment we can define the format. Here is one sample to show one decimal place autopct='%1.1f%%' , for two decimal places it can be autopct='%1.2f%%'
df.plot.pie(title="Colors",y='MATH',
        autopct='%1.1f%%')

frame

Axes frame is added with the chart if True. Default value is False

Saving graph as image

plot=df.plot.pie(title="Colors",y='MATH',autopct='%1.1f%%')
fig = plot.get_figure()
fig.savefig("D:\\my_data\\output2.png")
Pandas plot Line plot Bar plot Box plot Density plot Area plot Scatter Plot Hexbin plot
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer