Home > other >  Pie chart enclosed with a black line (rectangle)
Pie chart enclosed with a black line (rectangle)

Time:07-11

Below you can see my data and facet plot in matplotlib.

import pandas as pd
import numpy as np
pd.set_option('max_columns', None)
import matplotlib.pyplot as plt
import matplotlib as mpl

# Data

data = {
         'type_sale': ['g_1','g_2','g_3','g_4','g_5','g_6','g_7','g_8','g_9','g_10'],
         'open':[70,20,24,150,80,90,60,90,20,20],
         'closed':[30,14,20,10,20,40,10,10,10,10],
        }

df = pd.DataFrame(data, columns = ['type_sale',
                                   'open',
                                   'closed',
                                   ])

data1 = {
         'type_sale': [ 'open','closed'],
         'structure':[70,30],
                 }

df1 = pd.DataFrame(data1, columns = ['type_sale',
                                   'structure',
                                   ])



# Ploting
labels = ['open','closed']

fig, axs = plt.subplots(2,2, figsize=(10,8))
plt.subplots_adjust(wspace=0.2, hspace=0.6)
df1.plot(x='type_sale', y='structure',labels=labels,autopct='%1.1f%%',kind='pie', title='Stacked Bar Graph by dataframe',ax=axs[0,0])
df.plot(x='type_sale', kind='bar', stacked=True, title='Stacked Bar Graph by dataframe', ax=axs[0,1])
df.plot(x='type_sale', kind='bar', stacked=True, title='Stacked Bar Graph by dataframe',ax=axs[1,0])
df.plot(x='type_sale', kind='bar', stacked=True,title='Stacked Bar Graph by dataframe', ax=axs[1,1])

plt.suptitle(t='Stacked Bar Graph by dataframe', fontsize=16)
plt.show()

If you compare the first pie plot with others, you can spot a big difference. Namely, the first pie plot is not enclosed with a black line (rectangle), while the other is enclosed.

enter image description here So can anybody help me with how to solve this problem?

CodePudding user response:

After playing around myself, it seems that this is working, but I think the pie gets stretched, which doesn't look that good.

EDIT found a better solution with set_adjustable

also two options how you create the piechart, the frame and ticks differ in a bit.

# 1
axs[0,0].pie(df1['structure'],labels=labels,autopct='%1.1f%%',frame=True,radius=10) 
axs[0,0].set_title('Stacked Bar Graph by dataframe')

# 2
df1.plot(x='type_sale', y='structure',labels=labels,autopct='%1.1f%%',kind='pie', title='Stacked Bar Graph by dataframe',ax=axs[0,0])

axs[0,0].set_frame_on(True)
axs[0,0].set_adjustable('datalim')

  • Related