Removing labels from a pie chart moves the legend box

I am trying to make a pie chart where I remove the labels from it. I am currently doing this:

qx = queens_tree_types.plot(ax=axes[0], kind='pie', figsize=(8,30), legend=True, 
                            autopct='%1.0f%%', pctdistance=0.9, radius=1.2)
axes[0].set_title('Queens');

      

Which gives me the following pie:

enter image description here

That's fine and that's it, but I want to remove the labels from the diagram. When I try to do it simply labels=None

, I get the following image:

enter image description here

The original pie chart shows the legends installed as follows:

qx.legend(bbox_to_anchor=(2.5, 1.05),
      ncol=2, fancybox=True, shadow=True)

      

But when I remove the shortcuts, I cannot move the legend window at all. What gives?

+3


source to share


1 answer


Consider placing the legend next to the pie chart as the default placement of the legend overlaps on top of the pie in the same figure. The dummy data is shown below:

Data

import pandas as pd
import numpy as np

df = pd.melt(pd.DataFrame(np.random.randint(0,10,size=(20, 10)), 
                          columns=['Hulkeberry Finn', 'Captain Ahab', 'Hester Prynne', 
                                   'Nick Carraway', 'Bigger Thomas', 'Scout Finch', 
                                   'Invisible Man', 'Denver',
                                   'Tom Joad', 'Edna Pontellier']),
             var_name='group')
df = df.groupby(['group']).sum()

      

Pie Graph 1 (with default legend overlay)

from matplotlib import rc, pyplot as plt

# GENERAL STYLE SETTINGS
font = {'family' : 'arial', 'weight': 'bold', 'size': 10}
rc('font', **font); rc("figure", facecolor="white"); rc('axes', edgecolor='darkgray')

# GRAPH WITH LEGEND
qx = df.plot(kind='pie', figsize=(8,8), y='value', labels=None,
             autopct='%1.0f%%', pctdistance=0.9, radius=1.2)
plt.legend(loc="center right", fontsize=10)

plt.title('Pie Chart Demonstration', weight='bold', size=14)

plt.show()
plt.clf()
plt.close()

      



Legend output of an envelope diagram

Pie Graph 2 (with adjacent subtitle)

plt.gca().axis("equal")
pie = plt.pie(df, startangle=0, autopct='%1.0f%%', pctdistance=0.9, radius=1.2)
labels=df.index.unique()
plt.title('Pie Chart Demonstration', weight='bold', size=14)
plt.legend(pie[0],labels, bbox_to_anchor=(1,0.5), loc="center right", fontsize=10, 
           bbox_transform=plt.gcf().transFigure)
plt.subplots_adjust(left=0.0, bottom=0.1, right=0.85)

plt.show()
plt.clf()
plt.close()

      

Pie chart subtitle legend output

+3


source







All Articles